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}`.}
添加回答
舉報