13

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
  • 30,582
  • 12
  • 56
  • 83
dutt
  • 7,909
  • 11
  • 52
  • 85
  • 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

6

You can also try the below:

import os  
if not os.path.exists(file_path):
    print "Path of the file is Invalid"
aquavitae
  • 17,414
  • 11
  • 63
  • 106
Vidz
  • 545
  • 1
  • 6
  • 16
  • 10
    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 –  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 Jul 22 '20 at 06:52
  • OP: "The file doesn't have to exist now, I'm wondering if it could exist.". So this test would claim that r"D:\Windows" was invalid, when what it is actually reacting to is merely that the folder does not exist. – kfsone Dec 19 '22 at 18:35
  • @Richard `£@/somewhere**spilled_coffee_on_the_keyboard` is a perfectly valid pathname, at least on non-Windows (filesystems). – umläute Jan 11 '23 at 06:53
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
  • 1,047
  • 8
  • 18
  • Just be aware that this will return "True" for a whole bunch of invalid paths on every system, you REALLY have to go out of your way to give this something it will reject, like injecting null bytes into the filename. – kfsone Dec 19 '22 at 18:48
0

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
  • 41,746
  • 15
  • 73
  • 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
  • 21
    **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