-1

Getting errors like

FileNotFoundError: [Errno 2] No such file or directory: '/path/to/files/file.pdf'

when trying to loop through and open files in a mounted FTP drive (mounted via curlftpfs 'myuser:mypassword'@MY.SERVER.IP /path/to/files). I suspect sync issues, as the mounted drive is from another server on our network.

I can see that the file is there, can open it manually, can ls '/path/to/files/file.pdf' to see the file, but when executing...

FILES = os.listdir('/path/to/files')
FILES.sort()
.
.
.
for file in FILES:
    with open(os.path.join('/path/to/files', 'file.pdf'), 'rb') as fd:
        do stuff

... I sometimes get the FileNotFOundError.

More confusing, I can actually open this file (using the same path string that the error message tells me is not a file or directory) separately by just starting a python interactive shell and run something like...

fd = open('/path/to/files/file.pdf', 'rb')
fd.read()

...so IDK what the issue could be when reading it in a list of files.

Any debugging ideas or ideas of what could be causing this? Could there be some kind of timing/sync issues between reading the files on the mounted FTP drive vs the script that is running locally (and how to fix)?

* UPDATE:

Oddly, printing the target path before trying to open the file like...

print(os.path.join('/path/to/files', 'file.pdf'))
time.sleep(2)  # giving even more time after initial access
with open(os.path.join('/path/to/files', 'file.pdf'), 'rb') as fd:
    do stuff

...seems to help (kinda). Now also randomly throws PermissionErrors for random files that I had no problem reading before (still occasionally throws FileNotFoundErrors) and that I can actually open when accessing individually in python interactive shell. Makes me moreso think it is some kind of sync issue. Will need to investigate more.

1 Answer 1

0

It seems that os.path is a module, it will return an error if you use it like os.path('/path/to/files/file.pdf') But I think it's not the cause of FileNotFOundError.

3
  • Sorry, that was a typo in my post. Edited Aug 13, 2021 at 3:31
  • I'm not sure, maybe i'll double check if there's a problem with os.path.join('/path/to/files', 'file.pdf') ; if print(os.path.join('/path/to/files', 'file.pdf')) and ls (the path), can you see the file as well?
    – ying li
    Aug 13, 2021 at 3:48
  • hmmm, printing the target path before trying to open it actually seems to help (kinda; now randomly throws PermissionErrors for random files). Makes me think it is some kind of sync issue. Will need to investigate more. Aug 13, 2021 at 4:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.