0

I'm trying to check if a path exists so it creates it if it doesn't exist but it always runs despite the folder existing. i.e. evaluating to false:

if not os.path.isdir(('thumnail\\' + folderName).replace('\\', '/')):
    os.makedirs('thumbnail\\' + folderName)

Whats the correct way to do this. I've tried using path.exists, changing the escaped '\' to '/' and not doing it but its always false.

Deeswoc
  • 159
  • 1
  • 13

2 Answers2

0

To check path validity in python use

os.path.exists('Absolute or Relative path of directory or file')

the problem with your code maybe that 'thumnail\' isn't an absolute path or relative path, in that format it maybe referring to the current working directory.

0

I think you are using windows that's why you are using backward slash (\). It will work fine if you use forward slash as well (/)

Also, there is one spelling mistake thumnail and if you are using forward slash in the first line then use it same for second as well.

if not os.path.isdir(('thumbnail\\' + folderName).replace('\\', '/')):
    os.makedirs(('thumbnail\\' + folderName).replace('\\', '/'))

Also, try using python Pathlib package to make your code windows Linux environment independent.

There is a great tutorial here. - https://realpython.com/python-pathlib/

if you are windows make string raw type to avoid conflict with a backward slash as a escape character like this r'thumbnail\'

Mukul
  • 860
  • 8
  • 19