【发布时间】:2017-07-29 14:32:22
【问题描述】:
我正在尝试在 while 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]
【问题讨论】:
-
你好像忘了问一个问题。这段代码除了缩进还有什么问题?
-
os.listdir(".") == False这几乎没用...os.listdir返回一个列表,所以这永远不是False -
if i('A', 'B') == False:也是错误的。i是一个字符串,而不是一个函数。请阅读有关 python 的更多信息。 -
你的代码可能在不同的时空。
标签: python file directory exists