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 (" ")
S. Me
  • 31
  • 1
  • 9

2 Answers2

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 (" ")
everfight
  • 420
  • 3
  • 10
  • 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`. – ekhumoro Mar 11 '20 at 11:29
  • 1
    Please add some explanation with your code. – Itamar Mushkin Mar 11 '20 at 11:33
  • thought that was not so hard to understand... well thanks – S. Me Mar 11 '20 at 12:19
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.

  • 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"/) – S. Me Mar 11 '20 at 12:43