Check whether a path is valid in Python

python

Solution

Attempting it first is the best way, I recommend doing that.

try:
    open(filename, 'w')
except OSError:
    # handle error here

I believe you'll get OSError, catch that explicitly, and test on the platform you're using this on.

Problem

Is there any easy way to check whether a path is valid? The file doesn't have to exist now, I'm wondering if it could exist. my current version is this: ``` try: f = open(path) except: <path invalid> ``` I'm considering simply checking whether the path contains any of these characters.

Original source

Related problems