[New-bugs-announce] [issue36243] Python os.listdir fails with FileNotFoundError when directory exists

Geoff Alexander report at bugs.python.org
Fri Mar 8 12:22:58 EST 2019


New submission from Geoff Alexander <gdlxn at us.ibm.com>:

I have the following code:

```
    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)
```

Occasionally, the call to os.listdir(dir) fails with FileNotFoundError even though the os.path.exists(dir) call says that exists:

```
    08:57:56 - C:\r2g-wd\sport-6.0.5\SBS\SBS\Light\bin\com\ibm\ArtifactTechnology\ABS\ArtifactBroker exists
    Traceback (most recent call last):
      File "migration.py", line 169, in 
        migrate()
      File "migration.py", line 80, in migrate
        rtc.acceptchangesintoworkspace(rtc.getchangeentriestoaccept(changeentries, history))
      File "c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git-repositories\rtc2git-migration-tool\rtcFunctions.py", line 304, in acceptchangesintoworkspace
        Commiter.handle_empty_directories(os.getcwd())
      File "c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git-repositories\rtc2git-migration-tool\gitFunctions.py", line 126, in handle_empty_directories
        Commiter.handle_empty_directories(full_entry)
      File "c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git-repositories\rtc2git-migration-tool\gitFunctions.py", line 126, in handle_empty_directories
        Commiter.handle_empty_directories(full_entry)
      File "c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git-repositories\rtc2git-migration-tool\gitFunctions.py", line 126, in handle_empty_directories
        Commiter.handle_empty_directories(full_entry)
      [Previous line repeated 6 more times]
      File "c:\Users\GeoffAlexander\Documents\Nirvana\RTC2Git\git-repositories\rtc2git-migration-tool\gitFunctions.py", line 116, in handle_empty_directories
        entries = os.listdir(dir)
    FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\r2g-wd\\sport-6.0.5\\SBS\\SBS\\Light\\bin\\com\\ibm\\ArtifactTechnology\\ABS\\ArtifactBroker'
```

I've also seen a similar FileNoteFound on the 

```
            open(os.path.join(dir, Commiter.hed_file), "w").close()
```

line when os.path.exists(dir) says that the directory exists.

How can this happen? I'm running Python 3.7.2 64-bit on Windows 10.

----------
components: Windows
messages: 337516
nosy: Geoff.Alexander, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: Python os.listdir fails with FileNotFoundError when directory exists
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36243>
_______________________________________


More information about the New-bugs-announce mailing list