Home > database >  How can I write the path in Python on a Windows?
How can I write the path in Python on a Windows?

Time:10-23

I'm currently trying to set up a webdriver, and I just realised the reason it's not working is because I can't get the path to work. Currently, I'm trying basic code:

file = "D:\\"
if os.path.exists(file):
    print(True)
else:
    print(False)

And the answer is "False". Now, I do have a D:/ drive on my computer.

I tried r"D:", "D:\", "D:/", basically everything I could find in other articles. Has anybody else ever encountered this?

CodePudding user response:

file = "D:/"

Usually works, you could try a raw string literal if you are still having problems:

file = r'D:\'

You can also look into the excellent pathlib if you find yourself doing a lot of work with paths, such as constructing and joining. The documentation is available enter image description here

CodePudding user response:

You can use - 'C:/mydir' hope it should work.

  • Related