【发布时间】:2015-06-04 19:30:16
【问题描述】:
如果我有一个主文件夹和许多子文件夹,而我的目标文件位于这些子文件夹中。如何正确设置路径,然后程序可以直接通过我的主文件夹获取这些目标文件?
例如,
Main_folder
>sub_1
>>sub_1_v1
>>>targeted_file.txt # file I need
>>sub_2_v2
>>>targeted_file.txt # file I need
>sub_2
>>sub_1_v1
>>>targeted_file.txt # file I need
>>sub_2_v2
>>>targeted_file.txt # file I need
这是由 Julien Spronck (Grabbing data from certain files) 创建的程序
def get_all_files(path):
## get a generator with all file names
import os
import glob
return glob.iglob(os.path.join(path,'*.txt'))
def get_all_data(files):
## get a generator with all the data from all the files
for fil in files:
with open(fil, 'r') as the_file:
for line in the_file:
yield line
def write_lines_to_file(lines, outfile):
with open(outfile, 'w') as the_file:
for line in lines:
the_file.write(line+'\n')
path = 'blah blah' # path should be given here!
outfile = 'blah.csv'
files = get_all_files(path)
lines = get_all_data(files)
write_lines_to_file(lines, outfile)
我的问题是,我怎样才能正确地给出路径(从主文件夹),然后我可以一次抓取所有目标文件?
谢谢。
【问题讨论】:
标签: python python-2.7 directory