python循环读取文件夹之中的文件(python循环读取文件)
Python使用for循环依次打开该目录下的各文件
import?os
path?=?r"F:\Python\第一周作业\task"
otherpath=r"F:\Python\其它目录"
for?filename?in?os.listdir(path):
????print(path,filename)
????fullname=os.path.join(path,filename)
????if?os.path.isfile(fullname):????????
??????????othername=os.path.join(otherpath,filename)??
??????????otherfile=open(othername,'wb')
??????????for?line?in?open(fullname,'rb'):
??????????????for?c?in?line:
??????????????????if?not?c.isdigit():otherfile.write(c)
??????????otherfile.close()
请问大神怎样用python批量读取文件夹下的文件?
import?os
def?search(s,?path=os.path.abspath('.')):
????for?z?in?os.listdir(path):
????????if?os.path.isdir(path?+?os.path.sep?+?z):
????????????print('Currnet:',?path)
????????????path2?=?os.path.join(path,?z)
????????????print('future:',?path2)
????????????search(s,?path2)
????????elif?os.path.isfile(path?+?os.path.sep?+?z):
????????????if?s?in?z:
????????????????print(os.path.join(path,?z))
????????????????with?open(path?+?os.path.sep?+?z,?'r')?as?fr:
????????????????????with?open('save.txt',?'a')?as?fw:
????????????????????????fw.write(path?+?'\t'?+?fr.read())
search('csv',?'.')
python如何实现for循环操作文件?
python用for循环遍历文件操作,代码如下:
#!\urs\bin\env?python
#encoding:utf-8???????#设置编码方式??
import?os
import?re
class?loop_file:
????def?__init__(self,?root_dir,?short_exclude=[],?long_exclude=[],?file_extend=[]):
????????self.root_dir?=?root_dir
????????self.short_exclude?=?short_exclude
????????self.long_exclude?=?long_exclude
????????self.file_extend?=?file_extend
????def?__del__(self):
????????pass
????def?start(self,?func):
????????self.func?=?func
????????return?self.loop_file(self.root_dir)????
????def?loop_file(self,?root_dir):
????????t_sum?=?[]
????????sub_gen?=?os.listdir(root_dir)
????????for?sub?in?sub_gen:
????????????is_exclude?=?False
????????????for?extends?in?self.short_exclude:??##在不检查文件、目录范围中
????????????????if?extends?in?sub:??????????????##包含特定内容
????????????????????is_exclude?=?True
????????????????????break
????????????????if?re.search(extends,?sub):?????##匹配指定正则
????????????????????is_exclude?=?True
????????????????????break????????????????????
????????????if?is_exclude:
????????????????continue????????????
????????????abs_path?=?os.path.join(root_dir,?sub)
????????????is_exclude?=?False
????????????for?exclude?in?self.long_exclude:
????????????????if?exclude?==?abs_path[-len(exclude):]:
????????????????????is_exclude?=?True
????????????????????break
????????????if?is_exclude:
????????????????continue
????????????if?os.path.isdir(abs_path):
????????????????t_sum.extend(self.loop_file(abs_path))
????????????elif?os.path.isfile(abs_path):????????????
????????????????if?not?"."?+?abs_path.rsplit(".",?1)[1]?in?self.file_extend:??##不在后缀名?检查范围中
????????????????????continue
????????????????t_sum.append(self.func(abs_path))
????????return?t_sum????
if?'__main__'==__name__:
????root_dir?=?r'D:\harness\newshoppingcart\testcase\promo\single_promo'
????short_exclude?=?['.svn',?'.*_new.rb']?????###不包含检查的短目录、文件
????long_exclude?=?[]?????????????????????????###不包含检查的长目录、文件
????file_extend?=?['.rb']?????????????????????###包含检查的文件类型
????lf?=?loop_file(root_dir,?short_exclude,?long_exclude,?file_extend)
????for?f?in?lf.start(lambda?f:?f):
????????print?f
python中怎么读取文件内容
用open命令打开你要读取的文件,返回一个文件对象
然后在这个对象上执行read,readlines,readline等命令读取文件
或使用for循环自动按行读取文件