10

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.

Jim Ferrans
  • 29,992
  • 12
  • 55
  • 83
dutt
  • 7,415
  • 11
  • 50
  • 81
  • 2
    Possible duplicate of [Check whether a path is valid in Python without creating a file at the path's target](http://stackoverflow.com/questions/9532499/check-whether-a-path-is-valid-in-python-without-creating-a-file-at-the-paths-ta) – Cecil Curry Dec 05 '15 at 03:46

3 Answers3

7

You can also try the below:

import os  
if not os.path.exists(file_path):
    print "Path of the file is Invalid"
aquavitae
  • 15,598
  • 8
  • 60
  • 102
Vidz
  • 519
  • 1
  • 6
  • 16
  • 9
    This just reports whether a file exists. The question specifically asks about whether a path is *valid*, not whether it exists. – aquavitae Jan 06 '14 at 09:53
  • If the file exists, then its a valid path naturaully and it does exist – Vidz Jan 06 '14 at 10:31
  • Is not really a formal fallacy but a problem of definition. A path is valid when it can exist, i.e. a path that exist or that does not necessarily exist, but can be created if required. – SeF Oct 25 '16 at 14:59
  • According to 3.5.7 documentation: "Return True if path refers to an existing path or an open file descriptor." This would solve the question as asked. https://docs.python.org/3.5/library/os.path.html#os.path.exists – Steve Isenberg Oct 22 '19 at 18:46
  • To echo aquavitae. I'm accepting user input. I want to check that the path is valid (the OP's question). Path "/Users/billybob/config.txt" is valid whether it exists or not. Path "£@/somewhere**spilled_coffee_on_the_keyboard" is not valid, and never could be. – Richard Lyon Jul 22 '20 at 06:52
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.

Jerub
  • 40,254
  • 15
  • 71
  • 90
  • 1
    +1 and just for reference it's `IOError: [Errno 22] invalid mode ('w') or filename: ...` – mechanical_meat Nov 05 '10 at 01:46
  • 19
    **Problematic answer.** If `filename` does _not_ exist, this solution silently creates it as a 0-byte file. (_That's probably bad._) If `filename` is an existing file, this solution silently truncates it to a 0-byte file. (_That's definitely bad._) If `filename` is an existing directory, this solution raises an exception that will need to be differentiated from the desired `[Errno 22]` `IOError` exception described by [bernie](https://stackoverflow.com/users/42346/bernie) above. (_That's at least annoying._) In short, you probably do _not_ want to try this. – Cecil Curry Dec 05 '15 at 03:40
1

From python 3.4, to check if path is valid, you can also use pathlib module to do this:

from pathlib import Path

try:
    Path(file_path).resolve()
    exist = True
except (OSError, RuntimeError):
    exist = False
Kaz
  • 869
  • 6
  • 16