Python os.listdir fails with FileNotFoundError when directory exists The Next CEO of Stack OverflowHow to list only top level directories in Python?How to get full path of current file's directory in Python?How to know/change current directory in Python shell?os.listdir() showing file actually not in the folder on Python and Windowspython FileNotFoundError when using pytesseractFileNotFoundError File does not existFileNotFoundError on pythonCan os.listdir() method list remote directories in python?Python - FileNotFoundError for existing filepython os.listdir no such file or directory
Can I board the first leg of the flight without having final country's visa?
Can this note be analyzed as a non-chord tone?
Is a distribution that is normal, but highly skewed, considered Gaussian?
Are the names of these months realistic?
Expectation in a stochastic differential equation
Strange use of "whether ... than ..." in official text
Why don't programming languages automatically manage the synchronous/asynchronous problem?
How to find image of a complex function with given constraints?
Expressing the idea of having a very busy time
Does Germany produce more waste than the US?
Spaces in which all closed sets are regular closed
Graph of the history of databases
Is it ever safe to open a suspicious HTML file (e.g. email attachment)?
Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico
Film where the government was corrupt with aliens, people sent to kill aliens are given rigged visors not showing the right aliens
Could a dragon use its wings to swim?
Can I calculate next year's exemptions based on this year's refund/amount owed?
From jafe to El-Guest
Yu-Gi-Oh cards in Python 3
How did Beeri the Hittite come up with naming his daughter Yehudit?
Does destroying a Lich's phylactery destroy the soul within it?
Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact
How to Implement Deterministic Encryption Safely in .NET
How to get the last not-null value in an ordered column of a huge table?
Python os.listdir fails with FileNotFoundError when directory exists
The Next CEO of Stack OverflowHow to list only top level directories in Python?How to get full path of current file's directory in Python?How to know/change current directory in Python shell?os.listdir() showing file actually not in the folder on Python and Windowspython FileNotFoundError when using pytesseractFileNotFoundError File does not existFileNotFoundError on pythonCan os.listdir() method list remote directories in python?Python - FileNotFoundError for existing filepython os.listdir no such file or directory
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 it exists:
08:57:56 - C:r2g-wdsport-6.0.5SBSSBSLightbincomibmArtifactTechnologyABSArtifactBroker exists
Traceback (most recent call last):
File "migration.py", line 169, in <module>
migrate()
File "migration.py", line 80, in migrate
rtc.acceptchangesintoworkspace(rtc.getchangeentriestoaccept(changeentries, history))
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolrtcFunctions.py", line 304, in acceptchangesintoworkspace
Commiter.handle_empty_directories(os.getcwd())
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
[Previous line repeated 6 more times]
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.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'
How can this happen? I'm running Python 3.7.2 64-bit on Windows 10.
python-3.x windows filesystems
add a comment |
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 it exists:
08:57:56 - C:r2g-wdsport-6.0.5SBSSBSLightbincomibmArtifactTechnologyABSArtifactBroker exists
Traceback (most recent call last):
File "migration.py", line 169, in <module>
migrate()
File "migration.py", line 80, in migrate
rtc.acceptchangesintoworkspace(rtc.getchangeentriestoaccept(changeentries, history))
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolrtcFunctions.py", line 304, in acceptchangesintoworkspace
Commiter.handle_empty_directories(os.getcwd())
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
[Previous line repeated 6 more times]
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.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'
How can this happen? I'm running Python 3.7.2 64-bit on Windows 10.
python-3.x windows filesystems
Note that the underlying Windows error isERROR_PATH_NOT_FOUND
(3), notERROR_FILE_NOT_FOUND
(2). This means the problem in the example is with a path component prior to "ArtifactBroker".
– eryksun
Mar 9 at 7:31
Style note: an empty list has a false boolean value, so the idiomatic test isnot entries
instead ofentries == []
.
– eryksun
Mar 9 at 7:33
add a comment |
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 it exists:
08:57:56 - C:r2g-wdsport-6.0.5SBSSBSLightbincomibmArtifactTechnologyABSArtifactBroker exists
Traceback (most recent call last):
File "migration.py", line 169, in <module>
migrate()
File "migration.py", line 80, in migrate
rtc.acceptchangesintoworkspace(rtc.getchangeentriestoaccept(changeentries, history))
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolrtcFunctions.py", line 304, in acceptchangesintoworkspace
Commiter.handle_empty_directories(os.getcwd())
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
[Previous line repeated 6 more times]
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.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'
How can this happen? I'm running Python 3.7.2 64-bit on Windows 10.
python-3.x windows filesystems
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 it exists:
08:57:56 - C:r2g-wdsport-6.0.5SBSSBSLightbincomibmArtifactTechnologyABSArtifactBroker exists
Traceback (most recent call last):
File "migration.py", line 169, in <module>
migrate()
File "migration.py", line 80, in migrate
rtc.acceptchangesintoworkspace(rtc.getchangeentriestoaccept(changeentries, history))
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolrtcFunctions.py", line 304, in acceptchangesintoworkspace
Commiter.handle_empty_directories(os.getcwd())
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.py", line 126, in handle_empty_directories
Commiter.handle_empty_directories(full_entry)
[Previous line repeated 6 more times]
File "c:UsersGeoffAlexanderDocumentsNirvanaRTC2Gitgit-repositoriesrtc2git-migration-toolgitFunctions.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'
How can this happen? I'm running Python 3.7.2 64-bit on Windows 10.
python-3.x windows filesystems
python-3.x windows filesystems
edited Mar 9 at 7:26
eryksun
23.8k25268
23.8k25268
asked Mar 8 at 17:10
Geoff AlexanderGeoff Alexander
488
488
Note that the underlying Windows error isERROR_PATH_NOT_FOUND
(3), notERROR_FILE_NOT_FOUND
(2). This means the problem in the example is with a path component prior to "ArtifactBroker".
– eryksun
Mar 9 at 7:31
Style note: an empty list has a false boolean value, so the idiomatic test isnot entries
instead ofentries == []
.
– eryksun
Mar 9 at 7:33
add a comment |
Note that the underlying Windows error isERROR_PATH_NOT_FOUND
(3), notERROR_FILE_NOT_FOUND
(2). This means the problem in the example is with a path component prior to "ArtifactBroker".
– eryksun
Mar 9 at 7:31
Style note: an empty list has a false boolean value, so the idiomatic test isnot entries
instead ofentries == []
.
– eryksun
Mar 9 at 7:33
Note that the underlying Windows error is
ERROR_PATH_NOT_FOUND
(3), not ERROR_FILE_NOT_FOUND
(2). This means the problem in the example is with a path component prior to "ArtifactBroker".– eryksun
Mar 9 at 7:31
Note that the underlying Windows error is
ERROR_PATH_NOT_FOUND
(3), not ERROR_FILE_NOT_FOUND
(2). This means the problem in the example is with a path component prior to "ArtifactBroker".– eryksun
Mar 9 at 7:31
Style note: an empty list has a false boolean value, so the idiomatic test is
not entries
instead of entries == []
.– eryksun
Mar 9 at 7:33
Style note: an empty list has a false boolean value, so the idiomatic test is
not entries
instead of entries == []
.– eryksun
Mar 9 at 7:33
add a comment |
1 Answer
1
active
oldest
votes
This is most likely a race condition. There is a period of time between testing if a directory exists, and then doing something to it, in which it can be deleted by another process.
The usual way to avoid this is to just try the action, and be ready for it to fail, e.g.:
try:
os.listdir(x)
except FileNotFoundError:
# log and stop here
This is not a race condition in my case. The directory exists before and after the failure.
– Geoff Alexander
Mar 8 at 18:23
Either way, do you get the same behaviour if you switch to a try/except model?
– match
Mar 8 at 18:43
Wrapping the failing calls in try/except worked!!! I no longer get the spurious FileNotFound exceptions. I'm still not sure why I need to wrap the calls in try/except to prevent the spurious FileNotFound exceptions. I've open Python Issue 36243 (bugs.python.org/…) to get a better understanding of what's going on.
– Geoff Alexander
Mar 9 at 6:55
add a comment |
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067904%2fpython-os-listdir-fails-with-filenotfounderror-when-directory-exists%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is most likely a race condition. There is a period of time between testing if a directory exists, and then doing something to it, in which it can be deleted by another process.
The usual way to avoid this is to just try the action, and be ready for it to fail, e.g.:
try:
os.listdir(x)
except FileNotFoundError:
# log and stop here
This is not a race condition in my case. The directory exists before and after the failure.
– Geoff Alexander
Mar 8 at 18:23
Either way, do you get the same behaviour if you switch to a try/except model?
– match
Mar 8 at 18:43
Wrapping the failing calls in try/except worked!!! I no longer get the spurious FileNotFound exceptions. I'm still not sure why I need to wrap the calls in try/except to prevent the spurious FileNotFound exceptions. I've open Python Issue 36243 (bugs.python.org/…) to get a better understanding of what's going on.
– Geoff Alexander
Mar 9 at 6:55
add a comment |
This is most likely a race condition. There is a period of time between testing if a directory exists, and then doing something to it, in which it can be deleted by another process.
The usual way to avoid this is to just try the action, and be ready for it to fail, e.g.:
try:
os.listdir(x)
except FileNotFoundError:
# log and stop here
This is not a race condition in my case. The directory exists before and after the failure.
– Geoff Alexander
Mar 8 at 18:23
Either way, do you get the same behaviour if you switch to a try/except model?
– match
Mar 8 at 18:43
Wrapping the failing calls in try/except worked!!! I no longer get the spurious FileNotFound exceptions. I'm still not sure why I need to wrap the calls in try/except to prevent the spurious FileNotFound exceptions. I've open Python Issue 36243 (bugs.python.org/…) to get a better understanding of what's going on.
– Geoff Alexander
Mar 9 at 6:55
add a comment |
This is most likely a race condition. There is a period of time between testing if a directory exists, and then doing something to it, in which it can be deleted by another process.
The usual way to avoid this is to just try the action, and be ready for it to fail, e.g.:
try:
os.listdir(x)
except FileNotFoundError:
# log and stop here
This is most likely a race condition. There is a period of time between testing if a directory exists, and then doing something to it, in which it can be deleted by another process.
The usual way to avoid this is to just try the action, and be ready for it to fail, e.g.:
try:
os.listdir(x)
except FileNotFoundError:
# log and stop here
answered Mar 8 at 17:26
matchmatch
3,1231925
3,1231925
This is not a race condition in my case. The directory exists before and after the failure.
– Geoff Alexander
Mar 8 at 18:23
Either way, do you get the same behaviour if you switch to a try/except model?
– match
Mar 8 at 18:43
Wrapping the failing calls in try/except worked!!! I no longer get the spurious FileNotFound exceptions. I'm still not sure why I need to wrap the calls in try/except to prevent the spurious FileNotFound exceptions. I've open Python Issue 36243 (bugs.python.org/…) to get a better understanding of what's going on.
– Geoff Alexander
Mar 9 at 6:55
add a comment |
This is not a race condition in my case. The directory exists before and after the failure.
– Geoff Alexander
Mar 8 at 18:23
Either way, do you get the same behaviour if you switch to a try/except model?
– match
Mar 8 at 18:43
Wrapping the failing calls in try/except worked!!! I no longer get the spurious FileNotFound exceptions. I'm still not sure why I need to wrap the calls in try/except to prevent the spurious FileNotFound exceptions. I've open Python Issue 36243 (bugs.python.org/…) to get a better understanding of what's going on.
– Geoff Alexander
Mar 9 at 6:55
This is not a race condition in my case. The directory exists before and after the failure.
– Geoff Alexander
Mar 8 at 18:23
This is not a race condition in my case. The directory exists before and after the failure.
– Geoff Alexander
Mar 8 at 18:23
Either way, do you get the same behaviour if you switch to a try/except model?
– match
Mar 8 at 18:43
Either way, do you get the same behaviour if you switch to a try/except model?
– match
Mar 8 at 18:43
Wrapping the failing calls in try/except worked!!! I no longer get the spurious FileNotFound exceptions. I'm still not sure why I need to wrap the calls in try/except to prevent the spurious FileNotFound exceptions. I've open Python Issue 36243 (bugs.python.org/…) to get a better understanding of what's going on.
– Geoff Alexander
Mar 9 at 6:55
Wrapping the failing calls in try/except worked!!! I no longer get the spurious FileNotFound exceptions. I'm still not sure why I need to wrap the calls in try/except to prevent the spurious FileNotFound exceptions. I've open Python Issue 36243 (bugs.python.org/…) to get a better understanding of what's going on.
– Geoff Alexander
Mar 9 at 6:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067904%2fpython-os-listdir-fails-with-filenotfounderror-when-directory-exists%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Note that the underlying Windows error is
ERROR_PATH_NOT_FOUND
(3), notERROR_FILE_NOT_FOUND
(2). This means the problem in the example is with a path component prior to "ArtifactBroker".– eryksun
Mar 9 at 7:31
Style note: an empty list has a false boolean value, so the idiomatic test is
not entries
instead ofentries == []
.– eryksun
Mar 9 at 7:33