简体   繁体   English

从计算机中收集 python 中的每个 txt 文件并打印

[英]Collecting every txt file from computer in python and printing it

I am trying to collect every txt file from my computer and write it into the terminal when I run the script.我正在尝试从我的计算机收集每个 txt 文件,并在运行脚本时将其写入终端。 I do not know how to do it.我不知道怎么做。 Is there a way to read every txt file in the computer then print the contents?有没有办法读取计算机中的每个txt文件然后打印内容? (not a certain folder or directory). (不是某个文件夹或目录)。

In Python, the glob module would give you a list of filenames matching a given string.在 Python 中, glob模块将为您提供与给定字符串匹配的文件名列表。 In your case, glob.glob('dir/*.txt') would give you a list of filenames in directory dir that end in.txt.在您的情况下, glob.glob('dir/*.txt')将为您提供目录dir中以.txt 结尾的文件名列表。 You can then open each file and print() it to the terminal.然后,您可以打开每个文件并将其print()到终端。 Depending on your OS, you might be able to do it in your terminal without writing a separate script.根据您的操作系统,您可能无需编写单独的脚本就可以在终端中执行此操作。

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

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