1

如果它不存在,我正在尝试创建一个子目录。这是我的函数的相关部分(TMP是常量“-InProg”):

def create_directories(parent, tmp_dir=""):
    """
    Terminates program if tmp_dir already exists; otherwise
    creates subdirectories that don't already exist
    """
    if not tmp_dir:
        tmp_dir = parent + TMP
    try:
        if os.path.exists(tmp_dir):
            LOGGER.error('Automation already running')
            return False
        else:
            os.makedirs(tmp_dir)
        <...snip...>
    except OSError:
        LOGGER.error('Directory %s not valid', parent, exc_info=True)
        sys.exit("ERROR: Directory " + parent + " not valid")

以下是日志的相关部分:

ERROR:Directory <parent> not valid
Traceback (most recent call last):
  File "</path/to/my/script>.py", line 139, in create_directories
    os.makedirs(tmp_dir)
  File "/usr/local/Cellar/python@2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 17] File exists: '<parent>-InProg'

显然tmp_dir已经存在。但如果tmp_dir存在,那么代码根本不应该到达os.makedirs(tmp_dir),对吧?然而,这绝对是引发 OSError 的特定行。

我能找到的唯一线索是os.path.exists()False的文档说:“在某些平台上,如果未授予对请求的文件执行os.stat()的权限,此函数可能会返回,即使路径物理存在。” 如果这是问题所在,我该怎么办?

注意:这个解释似乎有点不太可能,因为程序本身就是创建tmp_dir的,我无法想象它能够创建一个它没有权限的子目录stat()。不过,我想不出另一种解释。我是否错过了其他一些非常明显的错误?

4

0 回答 0