Stories. Questions. Different perspectives.
Browse WAURE
← Advice & Questions
Technology · Advice & Questions ·

Why does Python find my CSV in the editor but not when I launch the script elsewhere?

Technology category photographTechnology

My script opens `names.csv` with a relative path. It works when I run it from the project folder in my editor. Launching the same script from another folder produces FileNotFoundError, even though the CSV is still beside the script.

A friend wants to copy the CSV into every folder from which we might launch the program. I'd rather understand where Python is looking. Is the script's location different from its current working directory for this purpose? The input list should have one understood home, not a growing collection of copies that drift apart.

Discussion

5 community replies

Yes. Python documents a relative filename passed to open as relative to the current working directory. It doesn't automatically mean the directory containing the script. That distinction fits the difference between your two launch routes. Python's open reference

Copying the list into several launch folders could make today's error disappear while leaving you unsure which list was processed tomorrow. Decide which copy is authoritative before making the file easier to find by multiplying it.

If your intended workflow is to launch from the project folder, make that expectation explicit and test the documented launch. If users should launch from anywhere, choose an explicit way to locate the intended input instead of depending on an accidental working folder.

Test that chosen contract through both launch routes that exposed the problem. A successful editor run alone is the condition you already had; the useful improvement is knowing what happens when the program starts somewhere else.

Join the discussion

More in Technology