[英]Reading a text file from within a folder and saving the name of the folder if a particular string is found in the text file - Python
我大约有100个带有随机名称的文件夹,例如1,2,3,4,... 100。 在这些文件夹中,我有一些带有一些字符串的文本文件。 例如:sample.txt。
这些文本文件都具有相同的名称,但是位于不同的文件夹中。 我需要的是从这些文件夹中读取文件,并读取这些文件中的文本,然后打印或保存这些文本文件的位置。
我只知道如何从文件中读取行(如果该文件位于我的pwd中)并在其中查找内容。 我为此使用以下代码:
with open(r'Example.txt', 'r') as infile_txt:
for line in infile_txt:
if r"sample" in line:
print line
如何从文件夹内部读取文件并记录这些文件夹的名称?
您可以为此使用os。 例如:
import os
list_downloads = os.listdir("C:/Users/user/Downloads")
这将为您提供列表中的所有子文件夹和文件。 然后,您可以遍历列表以找到所需的子文件夹并重复操作。
import os
import re
from os.path import join, getsize
with open('output.txt','w') as out_file:
for root,subFolders, files in os.walk(r"C:\Users\USER007\Desktop\Python Scripts\Reading metadata\files"):
if r'MetaData.txt' in files:
with open(os.path.join(root, 'MetaData.txt'), 'r') as in_file:
for lines in in_file:
if r'THIS' and r'THAT' and r'THOSE' in lines:
print root
上面的代码为我工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.