导航:首页 > 编程语言 > python读取文件夹内容

python读取文件夹内容

发布时间:2022-07-31 05:36:18

python怎么读取某个文件夹下的全部文件

import os
path=raw_input('enter the path:')
os.chdir(path)
files=os.listdir(path)

㈡ python怎么读取文件夹内容

#encoding:utf-8
import os
#设置文件夹所在路径,我这里设置哦当前路径
path = './'
#列出路径下所有的一级目录+文件
files = os.listdir(path)
print files
#利用递归,列出目录下包括子目录所有的文件及文件夹(但是没有分级,如果需要分级,自己写吧)
files1 = []
def listfiles(path):
for i in os.listdir(path):
if os.path.isdir(path+i):
files1.append(i)
listfiles(path+i)
else:
files1.append(i)
listfiles(path)
print files1

㈢ python如何读取文件的内容

# _*_ coding: utf-8 _*_

import pandas as pd

# 获取文件的内容

def get_contends(path):

with open(path) as file_object:

contends = file_object.read()

return contends

# 将一行内容变成数组

def get_contends_arr(contends):

contends_arr_new = []

contends_arr = str(contends).split(']')

for i in range(len(contends_arr)):

if (contends_arr[i].__contains__('[')):

index = contends_arr[i].rfind('[')

temp_str = contends_arr[i][index + 1:]

if temp_str.__contains__('"'):

contends_arr_new.append(temp_str.replace('"', ''))

# print(index)

# print(contends_arr[i])

return contends_arr_new

if __name__ == '__main__':

path = 'event.txt'

contends = get_contends(path)

contends_arr = get_contends_arr(contends)

contents = []

for content in contends_arr:

contents.append(content.split(','))

df = pd.DataFrame(contents, columns=['shelf_code', 'robotid', 'event', 'time'])

(3)python读取文件夹内容扩展阅读:

python控制语句

1、if语句,当条件成立时运行语句块。经常与else, elif(相当于else if) 配合使用。

2、for语句,遍历列表、字符串、字典、集合等迭代器,依次处理迭代器中的每个元素。

3、while语句,当条件为真时,循环运行语句块。

4、try语句,与except,finally配合使用处理在程序运行中出现的异常情况。

5、class语句,用于定义类型。

6、def语句,用于定义函数和类型的方法。

㈣ python中怎么读取文件内容

用open命令打开你要读取的文件,返回一个文件对象
然后在这个对象上执行read,readlines,readline等命令读取文件
或使用for循环自动按行读取文件

㈤ 请问大神怎样用python批量读取文件夹下的文件

importos
defsearch(s,path=os.path.abspath('.')):
forzinos.listdir(path):
ifos.path.isdir(path+os.path.sep+z):
print('Currnet:',path)
path2=os.path.join(path,z)
print('future:',path2)
search(s,path2)
elifos.path.isfile(path+os.path.sep+z):
ifsinz:
print(os.path.join(path,z))
withopen(path+os.path.sep+z,'r')asfr:
withopen('save.txt','a')asfw:
fw.write(path+' '+fr.read())
search('csv','.')

㈥ python 随机读取文件夹内一个文本文件

来个批量读取文件的例子:

defread_test_data():
forparent,dirnames,filenamesinos.walk(pbx_test_path):
forfilenameinfilenames:
iffilename[:5]=="TEST_":
file_path=pbx_test_path+"\%s"%filename
#file_bak_path=pbx_test_path_bak+"\%s"%filename
ifos.path.isfile(file_path):
#printfile_path
read_by_file(file_path)
#shutil.file(file_path,file_bak_path)
try:
os.remove(file_path)
exceptWindowsError:
pass

defread_by_file(file_path):
file_object=open(file_path)
count=0
thefile=open(file_path,'rb')
whileTrue:
buffer=str(thefile.readline())
ifnotbuffer:
break
all_test_list=buffer.split(";")
all_test_list=all_test_list[:len(all_test_list)-1]#去除回车行
zian_str=""
value_str=""
fori,testinenumerate(all_test_list):
test_attr_list=test.split("=")
zian_str+=""+test_attr_list[0]+","
value_str+="'"+test_attr_list[1]+"',"
zian_str="("+zian_str[:len(zian_str)-1]+")"
value_str="("+value_str[:len(value_str)-1]+")"
sql_str="insertintopbx_test"+zian_str+"values"+value_str+""
#mssql.insert(sql_str)
session.execute(sql_str)
count+=buffer.count(' ')
commit()
thefile.close()

㈦ Python如何从文件读取数据

1.1 读取整个文件

要读取文件,需要一个包含几行文本的文件(文件PI_DESC.txt与file_reader.py在同一目录下)

PI_DESC.txt

3.1415926535
8979323846
2643383279
5028841971

file_reader.py

with open("PI_DESC.txt") as file_object:
contents = file_object.read()
print(contents)

我们可以看出,读取文件时,并没有使用colse()方法,那么未妥善的关闭文件,会不会导致文件收到损坏呢?在这里是不会的,因为我们在open()方法前边引入了关键字with,该关键字的作用是:在不需要访问文件后将其关闭

1.2文件路径

程序在读取文本文件的时候,如果不给定路径,那么它会先在当前目录下进行检索,有时候我们需要读取其他文件夹中的路径,例如:

㈧ 如何用Python打开指定的某个文件夹比如我要用Python打开D盘里的一个普通文件,该怎么做

操作系统一般不允许程序直接操作磁盘文件的,文件在磁盘上的读写都是由操作系统提供的,文件操作包括创建、删除、修改权限、读取、写入等。要想实现对文件的操作就需要请求操作系统打开一个文件对象,然后通过操作系统提供的接口来实现对文件读写的相关操作。读写文件是最常见的 I/O 操作,Python内置相关API可以帮助我们快速的实现文件读写操作。

文件对象
Python内置的 open() 函数可以用于打开指定文件 ,该函数会返回一个文件对象(该对象包含了当前文件拥有的属性信息,相当于文件句柄)。参数file为要创建或打开文件的文件名称,参数mode用于指定文件的打开模式(可选),参数buffering用于指定对文件做读写操作时是否使用缓冲区。在打开文件之后就可调用文件对象的属性和方法,完成对文件的读/写操作之后最后需要关闭该文件,通过文件对象的close() 函数来实现即可。

打开模式
文件打开是可以指定其打开的模式,该参数通过mode来指定【默认模式为只读(r)】。选择不同的打开模式决定了后续可以对文件进行操作,如果使用 r 模式打开的文件则只能读取文件而无法修改文件内容。open()提供了多种打开模式,下面列出了对应的打开模式和说明:

㈨ python 怎么读取文件夹中的数据

由于项目需求收集并使用过一些爬虫相关库,做过一些对比分析。以下是我接触过的一些库: Beautiful Soup。名气大,整合了一些常用爬虫需求。缺点:不能加载JS。 Scrapy。

㈩ python 怎么读取当前目录下指定文件

读文本文件
input = open('data', 'r')
#第二个参数默认为r
input = open('data')

读二进制文件
input = open('data', 'rb')

读取所有内容
file_object = open('thefile.txt')
try:
all_the_text = file_object.read( )
finally:
file_object.close( )

读固定字节
file_object = open('abinfile', 'rb')
try:
while True:
chunk = file_object.read(100)
if not chunk:
break
do_something_with(chunk)
finally:
file_object.close( )

读每行
list_of_all_the_lines = file_object.readlines( )
如果文件是文本文件,还可以直接遍历文件对象获取每行:
for line in file_object:
process line

阅读全文

与python读取文件夹内容相关的资料

热点内容
程序员那么可爱25集 浏览:747
服务器地址和ip地址一样不 浏览:662
php中括号定义数组 浏览:600
php打印堆栈 浏览:514
华为adb命令行刷机 浏览:963
人像摄影pdf 浏览:755
解压文件密码怎样重新设置手机 浏览:999
高考指南pdf 浏览:693
爬虫python数据存储 浏览:240
u盘怎么取消加密 浏览:429
567除以98的简便算法 浏览:340
pdf手机如何解压 浏览:15
python描述器 浏览:60
战地联盟3解压密码 浏览:805
s型命令 浏览:25
php年薪5年 浏览:71
如何上网上设个人加密账户 浏览:44
linux打开ssh服务 浏览:78
微信位置可以加密吗 浏览:470
算法蛮力法 浏览:438