我是python的新手。在下面的代码中,我试图从一个应用程序目录中提取所有打印语句,该目录包含许多用Java和xml编写的文件,并将它们写入另一个文件(.txt)。我有以下代码,但不起作用
import glob
import os, sys
folderpath= os.chdir("/.../.../MyApp")
for files in glob.glob('*.java'):
with open("/.../.../result.txt", "w") as f:
for filename in files:
filepath = os.path.join(path, filename)
with open(folderpath + filename) as currentFile:
text = currentFile.read()
if ('print' in text):
f.write('Found the word in ' + filename + ' in line: ' + text + '\n')
答案 0 :(得分:0)
更改(不执行下面的行,只分配它)
folderpath= os.chdir("/.../.../MyApp")
到
os.chdir("/.../.../MyApp")
此行中未定义的路径:
filepath = os.path.join(path, filename)