有没有办法确保将csv或一般文件加载到脚本中?我试图创建一个交互式菜单,我想确保菜单选项仅在有效文件加载到脚本时才运行:
#Load new data.
if choice == 1:
while True:
filename = input("Please enter .csv filename: ")
path = os.listdir(os.getcwd())
#If the file is in the path, the code will run
if filename in path[:len(path)]:
print("\nData has been loaded!\n")
loadcsv = pd.read_csv(filename)
break
else:
print("\nFile has not been found.")
#Check for errors.
if choice == 2:
if filename not in sys.modules:
print("Please upload a file")
else:
pass
这里我尝试使用代码:
if choice == 2:
if filename not in sys.modules:
print("Please upload a file")
else:
pass
但它只是说没有定义文件名。