【问题标题】:Using Raw_Input to Check Existence of Directory使用 Raw_Input 检查目录是否存在
【发布时间】:2015-04-29 06:56:41
【问题描述】:

我正在尝试设计一个代码块来检查用户输入的目录是否存在,如果存在则打印,如果不存在。

import os

foldername = raw_input ("Please enter the name of your desired folder: ")

if os.path.exists(foldername):
     print "Yes, your folder exists!"
else:
     print "Please re-enter your folder directory as it does not exist."

以上是我所拥有的,但它始终输出该目录不存在,尽管我知道它存在。

见解?

蟒蛇

【问题讨论】:

  • 你放绝对路径了吗? /home/username/dir/?
  • 谢谢!我对 Python 很陌生,这花了我一段时间才明白!

标签: python if-statement file-exists


【解决方案1】:

代码看起来没问题。

我相信您没有使用绝对路径。
尝试使用/home/username/dir/folder 而不是folder

【讨论】:

    猜你喜欢
    • 2017-07-14
    • 1970-01-01
    • 2022-07-21
    • 2017-09-11
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 2013-05-19
    • 2012-09-12
    相关资源
    最近更新 更多