如何确保将文件加载到脚本中

时间:2018-01-15 13:46:50

标签: python python-3.x file-upload python-interactive

有没有办法确保将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

但它只是说没有定义文件名。

0 个答案:

没有答案