3 回答

TA贡献1757条经验 获得超7个赞
尝试这个
import sys
import os
import random
import time
while True:
try:
user_input = input("Enter the path of your file (Remember to include the file name and extension): ") # Imports user defined file
if os.path.isfile(user_input):
print("I did not find the file at, "+str(user_input)) # Checks if file is present
continue
else:
break
except:
print('Error')
但是,不需要这么多代码......这就足够了
import sys
import os
import random
import time
while True:
user_input = input("Enter the path of your file (Remember to include the file name and extension): ") # Imports user defined file
if not os.path.isfile(user_input):
print("I did not find the file at, "+str(user_input))
continue
else:
# some functions
break

TA贡献1982条经验 获得超2个赞
最简单的,因为我不知道你为什么需要一个 try 语句:
import os
path = input("Enter the path of your file (Remember to include the file "
"name and extension)")
if os.path.exists():
pass
else:
print("I did not find the file at, "+str(path))

TA贡献1880条经验 获得超4个赞
import os
from builtins import input
while True:
user_input = input(
"Enter the path of your file (Remember to include the file name and extension): ")
if not os.path.isfile(user_input):
print(f"I did not find the file at {user_input}")
continue
else:
break
添加回答
举报