检查Python中的用户输入是否存在文件夹

时间:2017-03-08 20:24:39

标签: python file directory exists

我正在尝试在一段时间内进行一系列检查True循环,以查看文件夹是否存在或用户错误。在我的文件夹中,我有两个文件夹A和B.我会要求用户输入文件夹名称。

from os.path import exists

folder1_2 = input('Enter folder name: ')
i = (folder1, folder2)

while True:
    i = raw_input()
    does_it_exist = os.path.exists(i) # True/False
    if does_it_exist == False:
        print("The folder does not exist")            
        continue 
    if os.listdir(".") == False:
            print("folder not found")
        continue  
    if i('A', 'B') == False:
            print("not the same folder or user error")
        continue  
    break 
print("All tests passed successfully!")

return [folder1_2]

1 个答案:

答案 0 :(得分:0)

while True:
    f1 = input("Folder1?") #asking user for input
    f2 = input("Folder2?")
    if not os.path.exists(f1) or not os.path.exists(f2):
        print("some error message")
        continue
    if not os.path.isdir(f1) or not os.path.isdir(f2):
        print("another error message")
        continue
    if (f1 == f2): #are these folders the same
        print("one more error message")
        continue
    break
print("all tests passed successfully!")

我希望这会有所帮助。