3 回答
TA贡献1757条经验 获得超8个赞
如果您需要打开这些文件但不能将它们硬编码到程序中,您可能必须将它们作为命令行参数获取。然后你可以像这样执行你的程序:
$ python code.py ABC123 keys1.txt keys2.txt keys3.txt
TA贡献1815条经验 获得超10个赞
遍历每个 .txt 文件和该文件中的行(您尚未指定这些 txt 文件的外观)。检查与各种方法的匹配,我需要更多详细信息以及您到目前为止尝试提供的帮助。
TA贡献1828条经验 获得超3个赞
您可以使用os.listdir它来获取随后将遍历的文件列表。
参考: https: //docs.python.org/3/library/os.html#os.listdir
files_list = os.listdir('/path/to/keys') # ['keys1.txt', ...]
search_string = 'ABC1234'
found = False
for file in files_list:
if found:
break
with open(file, 'r') as fo:
for line in fo:
if line == search_string:
found = file
break
print(f'Found line `{search_string}` in file `{found}`.}
添加回答
举报
