【问题标题】:Python File Check and CreationPython 文件检查和创建
【发布时间】:2019-05-13 16:30:12
【问题描述】:

我正在尝试检查或创建目录,但出现此错误:

/usr/bin/python3.6 /home/user/Development/americas.py Traceback(大多数 最近通话最后):文件“/home/dquezada/Development/americas.py”, 第 23 行,在 os.mkdir(path) FileNotFoundError: [Errno 2] No such file or directory: '/home/dquezada/Development/data/maps/americas/'

进程以退出代码 1 结束

下面是我的代码:

path = '/home/user/Development/data/maps/americas/'

# Check if path exists, if it does not it creates it
if not os.path.exists(path):
    os.mkdir(path)

wm.render_to_file(path + timeStamp + '_americas.svg')

【问题讨论】:

    标签: python-3.x operating-system


    【解决方案1】:

    我相信os.path.isdir(path) 是您正在寻找的。 如果它是一个目录,这将返回 True,否则返回 False。 os.path.exists(path) 不适用于您的用途。

    path = '/home/user/Development/data/maps/americas/'
    
    # Check if path exists, if it does not it creates it
    if not os.path.isdir(path):
        os.mkdir(path)
    
    wm.render_to_file(path + timeStamp + '_americas.svg')
    

    这应该适合你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-05
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多