简体   繁体   English

在子目录的文件中串联多个.txt

[英]Concatenating multiple .txt in files in subdirectories

I have multiple .txt files in different levels of subdirectory. 我在不同级别的子目录中有多个.txt文件。 All txt files are in the final iteration, that is, there is no level with both .txt files and further directories. 所有txt文件都在最终迭代中,也就是说,.txt文件和其他目录都没有层次。 I want to concatenate them all into one new text file, but can't find a simple way to go through all the subdirectories. 我想将它们全部连接到一个新的文本文件中,但是找不到遍历所有子目录的简单方法。

A command I've found, that is to be entered into the python command line terminal thus: 我发现的一条命令要输入到python命令行终端中,因此:

$ cat source/file/*.txt > source/output/output.txt

But I am not sure how I could make this iterate over multiple subdirectories. 但是我不确定如何使它遍历多个子目录。 (I am a real beginner with python, there seems to be some confusion. Is this not a python command? The source I found it from claimed it was...) (我是python的真正初学者,似乎有些困惑。这不是python命令吗?我从源代码中发现它是……)

You could build them using a python script using something like: 您可以使用类似以下内容的python脚本来构建它们:

file_list = ['c:\data.txt', 'c:\folder1\data.txt']

for everything in file_list:
    f = open(everything)
    for line in f.readlines():
        print line

Call the script from cmd eg \\python.exe 'buildtxtfiles.py' 从cmd调用脚本,例如\\ python.exe'buildtxtfiles.py'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM