1

I'm on Debian using python3.7. I have a network drive that I typically mount to /media/N_drive with dir_mode=0777 and file_mode=0777. I generally have no issues with reading/writing files in this network drive.

Occasionally, especially soon after mounting the drive, if I try to run any Python script with os.getcwd() (including any imported libraries like pandas) I get the error FileNotFoundError: [Errno 2] No such file or directory. If I cd up to the local drive (cd /media/) the script runs fine.

Doing some reading, it sounds like this error indicates that the working directory has been deleted. Yet I can still navigate to the directory, create files, etc. when I'm in the shell. It only seems to be Python's os.getcwd() that has problems.

What is more strange is that this behavior is not predictable. Typically if I wait ~1 hour after mounting the drive the same script will run just fine.

I suspect this has something to do with the way the drive is mounted maybe? Any ideas how to troubleshoot it?

1 Answers1

1

To me, it seems a problem with the mount, e.g. the network disk will be disconnected, and reconnected. So your cwd is not more valid. Note: cwd is pointing to a disk+inode, it is not a name (which you will see). So /media/a is different to /media/a after a reconnection.

If you are looking on how to solve the mounting, you are in the wrong place. Try Unix&Linux sister site, or Serverfault (also a sister site).

If you are looking how to solve programmatically: save cwd at beginning of the script and use os.path.join() at every path access, so that you forcing absolute paths, and not relative paths, and so you should be on the correct location. This is not save, if you happen to read a file during disconnection.

Giacomo Catenazzi
  • 5,665
  • 1
  • 17
  • 25