← Debugging Guides

阅读中文版本

Python — FileNotFoundError with a path that exists

Fix FileNotFoundError by resolving the working directory, since relative paths depend on where the process was started.

beginnerdebuggingpythonfilesystem

Symptoms

  • FileNotFoundError: [Errno 2] No such file or directory for a path that exists
  • The script works from the project directory but fails elsewhere
  • The error appears when a service is started by systemd or a supervisor
  • The path is correct visually but not from the process working directory

Possible causes

  • A relative path is resolved against the process working directory, not the script location
  • The service manager sets a different working directory
  • The file is generated at runtime and the directory was not created
  • Case sensitivity or a trailing space in the name differs from the filesystem

Diagnosis steps

Print the working directory and resolved path

python -c "import os; print(os.getcwd()); print(os.path.abspath('data/x.csv'))"

Interpretation

The printed absolute path shows what the relative string actually resolves to, which is often the real culprit.

Confirm where the file truly is

ls -l "$(python -c 'import os; print(os.path.abspath("data/x.csv"))')"

Interpretation

If this fails while the file exists elsewhere, the process resolves the path differently than you assume.

Check the service working directory

systemctl cat myservice | grep -i workingdirectory

Interpretation

A service that starts in / has a different base for relative paths, so files that work in development disappear in production.

Root cause

The path string is relative, so the operating system resolves it against the process working directory. When that directory differs from where the file lives, the open fails even though the file exists somewhere.

Fix

Build paths from a known base such as the module location or an absolute configured root, set the service working directory explicitly, or create runtime directories before writing.

Verify the fix

Rerun the operation and confirm the file opens, and log the resolved absolute path to confirm it now points at the intended location.

Symptom walkthrough

A script raises FileNotFoundError for a path that clearly exists when inspected in a shell. In development the script is started from the project directory, so relative paths happen to work. In production a service manager or container may start the process from a different working directory, so the same relative string resolves to a different absolute location and the open fails.

Fix in practice

Anchor paths to a stable base: derive them from the file location or a configured absolute root, or set the working directory explicitly in the service definition. Create runtime directories before writing, and avoid relying on the shell being in a particular folder.

Verification

Rerun and confirm the file opens, and log the resolved absolute path to verify it points where you expect. The logged path turning out correct is stronger evidence than a single successful run.

Diagnostic commands

Check the behavior with commands like these:

python -c "import os; print(os.getcwd()); print(os.path.abspath('data/x.csv'))"
ls -l "$(python -c 'import os; print(os.path.abspath("data/x.csv"))')"
systemctl cat myservice | grep -i workingdirectory