Knowledgebase

Working With File Paths Print

  • python, troubleshooting, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Locating files reliably.

THE PROBLEM

Paths written for one system fail on another.

WHAT TO USE

Python's path handling, rather than joining strings manually.

WHY

It handles separators correctly on every platform.

ABSOLUTE AND RELATIVE PATHS

Absolute paths start from the root and always mean the same thing.

Relative paths depend on where the program was run from.

WHAT THAT CAUSES

A script working when run from its own folder and failing from elsewhere.

WHAT TO DO ABOUT IT

Build paths relative to the script's own location, not the current directory.

WHAT TO CHECK

Whether a path exists Whether it is a file or a directory

WHAT TO BE CAREFUL WITH

Paths from user input, which can point anywhere Creating directories that already exist Assuming a path separator

THE USER INPUT POINT

Never use a path supplied from outside without validating it.

That is how files outside the intended directory get read or written.

WHAT TO USE FOR TEMPORARY FILES

The facilities provided for them, which handle cleanup.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot