jan jan - 2 years ago 235
Python Question

Sanitizing a file path in python

I have a file browser application that exposes a directory and its contents to the users.

I want to sanitize the user input, which is a file path, so that it does not allow absolute paths such as '/tmp/' and relative paths such as '../../etc'

Is there a python function that does this across platforms?

Answer Source

Perhaps instead of just cleaning the path (blacklisting) you could only allow (whitelist) a valid path.

python provides a variety of tools in os.path to do this in an os independent way

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download