Phyton Check whether a path is valid in Python python check whether path exists,python check whether path is

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.

Answer:1

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.

Answer:2



  1. python check whether path exists
  2. python check whether path is valid
  3. python check if path exists
  4. python check if path is directory
  5. python check if path is valid
  6. python check if path is absolute
  7. python check if path is file
  8. python check if path is file or directory
  9. python check if path is relative
  10. python check if path exists and create
  11. python check if path is folder
  12. python check if path is subpath
  13. python check if path is relative or absolute
  14. python check if path contains directory
  15. python check if path is dir
  16. python check if path
  17. python check if path ends with slash
  18. python check if path is file or folder
  19. python check if path is writable
  20. python check if path is empty

I have a class defined as follows class M(object): def __init__(self, **kwargs): ...do_something and I have the result of argparse.parse_args(), for example: > args = parse_args() &...

I have a class defined as follows class M(object): def __init__(self, **kwargs): ...do_something and I have the result of argparse.parse_args(), for example: > args = parse_args() &...

I'm using PyDev ( with Aptana ) to write and debug a Python Pylons app, and I'd like to step through the tests in the debugger. Is it possible to launch nosetests through PyDev and stop at ...

I'm using PyDev ( with Aptana ) to write and debug a Python Pylons app, and I'd like to step through the tests in the debugger. Is it possible to launch nosetests through PyDev and stop at ...