1

I've got trouble to verify a path who contains variables using "os.path.exists(path)". Which character should be used to put the content of vars in path and then verify the path?

nb : EOF Error...

Thanks you.

path = (C:\programm\.....\+var1+\+var2+\)
isExist = os.path.exists(path)
if (isExist == false):
    os.mkdirs(path)
else:
    print (" ")
1
  • Sorry for my poor english.. Commented Mar 11, 2020 at 10:48

2 Answers 2

1
import os
# change \ to \\
path = ("C:\\programm\\.....\\+var1+\\+var2+\\")
isExist = os.path.exists(path)
# change false to False
if (isExist == False):
    # change os.mkdir to os.makedirs
    os.makedirs(path)
else:
    print (" ")
Sign up to request clarification or add additional context in comments.

3 Comments

Using exists like that is subject to a race condition, because another process could create the directory in between the two operations. It's more robust to eliminate the exists check by putting mkdir/makedirs in a try/except and catching FileExistsError.
Please add some explanation with your code.
thought that was not so hard to understand... well thanks
0

modify value of path variable as,

path = (C:\programm\.....\+var1+\+var2+\)

backslash is a escape character in programming languages, so to print backslash, we need to put double backslash(\) in variable value or print statement.

1 Comment

Just see that we can use a simple slash instead of double backslash and escape var in path with this : " . Example : path = (C:/.../.../"+var1+"/..../"+var2"/)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.