gpt4 book ai didi

python - 如何通过主文件夹从许多子文件夹中抓取文件?

转载 作者:太空宇宙 更新时间:2023-11-04 05:52:39 25 4
gpt4 key购买 nike

如果我有一个主文件夹和许多子文件夹,而我的目标文件位于这些子文件夹中。我怎样才能正确设置我的路径然后程序可以直接通过我的主文件夹获取这些目标文件?

例如,

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)

我的问题是,我怎样才能正确地给出路径(从主文件夹)然后我可以一次抓取所有目标文件?

谢谢。

最佳答案

要遍历文件夹,然后是文件,请使用:

import os                                                                                                             

def list_files(dir):
r = []
subdirs = [x[0] for x in os.walk(dir)]
for subdir in subdirs:
files = os.walk(subdir).next()[2]
if (len(files) > 0):
for file in files:
r.append(subdir + "/" + file)
return r

如图所示:

Python: Iterate through folders, then subfolders and print filenames with path to text file

关于python - 如何通过主文件夹从许多子文件夹中抓取文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29368982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com