【发布时间】:2016-07-26 02:48:32
【问题描述】:
我正在尝试查找目录下的文件中包含的字符串。然后让它将它的文件名和目录存储在一个新的文本文件或其他东西下。 我到达了它通过目录并找到一个字符串的位置,然后打印了一个结果。但不确定下一步。
请帮忙,我对编码和 python 完全陌生。
import glob, os
#Open a source as a file and assign it as source
source = open('target.txt').read()
filedirectories = []
#locating the source file and printing the directories.
os.chdir("/Users/a1003584/desktop")
for root, dirs, files in os.walk(".", topdown=True):
for name in files:
print(os.path.join(root, name))
if source in open(os.path.join(root, name)).read():
print 'treasure found.'
【问题讨论】:
-
您可能会发现python tutor list 更适合此类问题。
标签: python string-comparison file-comparison