如何递归遍历所有子目录并读取文件? [英] How to recursively go through all subdirectories and read files?

查看:195
本文介绍了如何递归遍历所有子目录并读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个根目录目录,其中包含多个子目录,所有子目录均包含文件名data.txt.我想做的是编写一个脚本,该脚本进入根"目录,然后读取所有子目录并读取子目录中的每个"data.txt",然后将每个data.txt文件中的内容写入输出文件.

I have a root-ish directory containing multiple subdirectories, all of which contain a file name data.txt. What I would like to do is write a script that takes in the "root" directory, and then reads through all of the subdirectories and reads every "data.txt" in the subdirectories, and then writes stuff from every data.txt file to an output file.

这是我的代码的片段:

import os
import sys
rootdir = sys.argv[1]

with open('output.txt','w') as fout:
    for root, subFolders, files in os.walk(rootdir):
        for file in files:
            if (file == 'data.txt'):
                #print file
                with open(file,'r') as fin:
                    for lines in fin:
                        dosomething()

我的dosomething()部分-如果仅针对一个文件运行该部分,我已经测试并确认它可以正常工作.我还确认了,如果我告诉它打印文件(注释行),则脚本将打印出"data.txt".

My dosomething() part -- I've tested and confirmed for it to work if I am running that part just for one file. I've also confirmed that if I tell it to print the file instead (the commented out line) the script prints out 'data.txt'.

现在,如果我运行它,Python会给我这个错误:

Right now if I run it Python gives me this error:

File "recursive.py", line 11, in <module>
    with open(file,'r') as fin:
IOError: [Errno 2] No such file or directory: 'data.txt'

我不确定为什么找不到它-毕竟,如果我取消对打印文件"行的注释,它会打印出data.txt.我在做什么错?

I'm not sure why it can't find it -- after all, it prints out data.txt if I uncomment the 'print file' line. What am I doing incorrectly?

推荐答案

您需要使用绝对路径,您的file变量只是没有目录路径的本地文件名. root变量就是该路径:

You need to use absolute paths, your file variable is just a local filename without a directory path. The root variable is that path:

with open('output.txt','w') as fout:
    for root, subFolders, files in os.walk(rootdir):
        if 'data.txt' in files:
            with open(os.path.join(root, 'data.txt'), 'r') as fin:
                for lines in fin:
                    dosomething()

这篇关于如何递归遍历所有子目录并读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆