2017-03-08 1 views
-2

폴더가 있는지 또는 사용자 오류인지를 확인하기 위해 while 루프에서 일련의 검사를 수행하려고합니다. 내 폴더에는 두 개의 폴더 A와 B가 있습니다. 사용자에게 폴더 이름을 입력하도록 요청할 것입니다.Python의 사용자 입력에서 폴더가 존재하는지 확인

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] 
+2

질문을 잊어 버린 것처럼 보입니다. 들여 쓰기 이외의이 코드의 문제점은 무엇입니까? –

+1

'os.listdir (".") == False'는 거의 쓸모가 없다 ...'os.listdir'는리스트를 반환하므로'False'가되지 않습니다. –

+1

'if i ('A', 'B') == 거짓 :'잘못되었습니다. 'i'는 문자열이 아니라 함수입니다. 파이썬에 대해 더 읽어보십시오. –

답변

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!") 

나는이 도움이되기를 바랍니다.

관련 문제