大家好我有问题我希望Python搜索我的所有硬盘。在D:\中有sifreler.txt但是Python说print(“找不到文件”)为什么?
hard_drive = 'D:\sifreler.txt' , 'E:\sifreler.txt' , 'F:\sifreler.txt' , 'G:\sifreler.txt' , 'H:\sifreler.txt' , 'I:\sifreler.txt'
hard_drive1 = hard_drive
while True:
try:
for i in hard_drive1:
sifre = open(i, "r")
sifree = sifre.readline()
if sifree == "8da0dd52e2ee6c04528ede0cd84e6770":
print("True")
except FileNotFoundError:
print("File Not Found")
答案 0 :(得分:0)
请使用2个斜杠。例如:X:\\
而不是X:\
更新:
此外,更改try/except
位置。如果某个文件不存在,则不会终止for
块:
while True:
for i in hard_drive1:
try:
sifre = open(i, "r")
sifree = sifre.readline()
if sifree == "8da0dd52e2ee6c04528ede0cd84e6770":
print("True")
except FileNotFoundError:
print("File Not Found")