Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 当目录存在时,Python os.listdir将失败,并出现FileNotFoundError_Python 3.x_Windows_Filesystems - Fatal编程技术网

Python 3.x 当目录存在时,Python os.listdir将失败,并出现FileNotFoundError

Python 3.x 当目录存在时,Python os.listdir将失败,并出现FileNotFoundError,python-3.x,windows,filesystems,Python 3.x,Windows,Filesystems,我有以下代码: def handle_empty_directories(dir): if os.path.exists(dir): shouter.shout("%s exists" % dir) else: shouter.shout("%s doesn't exists" % dir) entries = os.listdir(dir) if entries == []: open(os.path.join(di

我有以下代码:

def handle_empty_directories(dir):
    if os.path.exists(dir):
        shouter.shout("%s exists" % dir)
    else:
       shouter.shout("%s doesn't exists" % dir)
    entries = os.listdir(dir)
    if entries == []:
        open(os.path.join(dir, Commiter.hed_file), "w").close()
    else:
        if (len(entries) > 1) and (Commiter.hed_file in entries):
            os.remove(os.path.join(dir, Commiter.hed_file))
        for entry in entries:
            if entry not in Commiter.hed_ignore:
                full_entry = os.path.join(dir, entry)
                if (os.path.isdir(full_entry)):
                    Commiter.handle_empty_directories(full_entry)
偶尔,调用
os.listdir(dir)
失败,出现
FileNotFoundError
,即使
os.path.exists(dir)
调用表示它存在:

08:57:56-C:\r2g wd\sport-6.0.5\SBS\SBS\Light\bin\com\ibm\ArtifactTechnology\ABS\ArtifactBroker存在
回溯(最近一次呼叫最后一次):
文件“migration.py”,第169行,在
迁移()
文件“migration.py”,第80行,在migrate中
rtc.AcceptChangesToWorkSpace(rtc.getchangeentriestoaccept(changeentries,history))
文件“c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git repositories\RTC2Git migration tool\rtcFunctions.py”,第304行,位于AcceptChangesToWorkSpace中
handle\u空目录(os.getcwd())
handle\u empty\u目录中的文件“c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git repositories\RTC2Git migration tool\gitFunctions.py”第126行
提交者.句柄\u空\u目录(完整\u条目)
handle\u empty\u目录中的文件“c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git repositories\RTC2Git migration tool\gitFunctions.py”第126行
提交者.句柄\u空\u目录(完整\u条目)
handle\u empty\u目录中的文件“c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git repositories\RTC2Git migration tool\gitFunctions.py”第126行
提交者.句柄\u空\u目录(完整\u条目)
[上一行重复了6次]
handle\u empty\u目录中的文件“c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git repositories\RTC2Git migration tool\gitFunctions.py”,第116行
entries=os.listdir(dir)
FileNotFoundError:[WinError 3]系统找不到指定的路径:“C:\\r2g wd\\sport-6.0.5\\SBS\\SBS\\Light\\bin\\com\\ibm\\ArtifactTechnology\\ABS\\ArtifactBroker”

这怎么会发生?我在Windows 10上运行的是64位Python 3.7.2。

这很可能是竞争条件。在测试某个目录是否存在,然后对其执行某些操作(在这段时间内,它可以被另一个进程删除)之间有一段时间

通常避免这种情况的方法是尝试该操作,并做好失败的准备,例如:

try:
  os.listdir(x)
except FileNotFoundError:
  # log and stop here

这不是我的比赛条件。目录在失败之前和之后都存在。无论哪种方式,如果切换到try/except模型,是否会得到相同的行为?将失败的调用包装在try/except中工作!!!我不再得到虚假的FileNotFound异常。我仍然不确定为什么需要将调用包装在try/except中以防止虚假的FileNotFound异常。我打开了Python发行版36243(),以更好地了解正在发生的事情。请注意,底层Windows错误是
error\u PATH\u NOT\u FOUND
(3),而不是
error\u FILE\u NOT\u FOUND
(2)。这意味着示例中的问题在于“ArtifactBroker”之前的路径组件。样式说明:空列表具有错误的布尔值,因此惯用的测试是
不是entries
,而不是
entries=[]