from pathlib import Path
import os
folderpath = input("What is the absolute path to Superman's folder?")
mypath = Path(folderpath)
mypath = os.listdir(folderpath)
for files in mypath:
print ("Found one of Superman's's files..." + files)
#up to here, the code is good
for read in byfiles:
byfiles = ('mypath\\files')
byfiles.read_text()
if "Superman" in read:
print("Found Superman in file " + read)
我需要搜索输入路径中的文件,找到单词Superman,然后打印找到单词的位置。我无法使连接与.read_text()一起工作
这是输出:
What is the absolute path to Superman's folder?C:\Users\OneDrive\Documents\Superman_files
Found one of Superman's files...boring_document.txt
Found one of Superman's files...eggs.txt
Found one of Superman's files...hello.txt
Found one of Superman's files...secret_document.txt
Found one of Superman's files...spam.txt
任何帮助都将不胜感激
希望这有帮助。代码是不言自明的。所以我跳过了解释
os.listdir
返回字符串列表,以便您可以进行如下检查:如果要检查文件内容,可以使用:
相关问题 更多 >
编程相关推荐