#πŸ”’ valid file path

23 messages Β· Page 1 of 1 (latest)

warm trail
#

hello, is there a way to check if the file path I input is valid?
like if I input c:\Userssssss\x.txt it will see that it is invalid but if I just type x.txt or c;\Users\user\Desktop\x.txt it will be ok

delicate harnessBOT
#

@warm trail

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

warm trail
#

not if it exists\

#

os.path.isfile works

#

but I want to see if the dir is valid I think?

#
import os

if os.path.exists(r"c:\Users\x.txt"):
    print(True)
else:
    print(False)

returns False

foggy stump
#

use exists, yes.

foggy stump
#

in os.path that's done via something like dirname

warm trail
#

can you give me an example pls ?

foggy stump
#

in pathlib it'd be

p = Path(r"c:\Users\x.txt")
assert p.parent.exists(), p
warm trail
#

so it checks if c:\Users exists?

#

alr !

#

what does it do tho

#

like how do I get a True or False

foggy stump
#

p.parent.exists() returns a boolean

#

!docs pathlib.Path.exists

delicate harnessBOT
#

Path.exists(*, follow_symlinks=True)```
Return `True` if the path points to an existing file or directory.

This method normally follows symlinks; to check if a symlink exists, add the argument `follow_symlinks=False`...
warm trail
#

I see

#

tyy

#

!close

delicate harnessBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.