I want to check if a file exists and if it does give the folder i create with mkdir
the next higher number.
Somehow I get the Error: AttributeError: 'module' object has no attribute 'exist'
I do not understand why that os function does not work for me. Any ideas?
import os
map_name="Example.png"
wk_dir = os.path.dirname(os.path.realpath('__file__'))
dir_name=os.path.splitext(os.path.basename(map_name))[0]
for n in range(0,200):
m=n+1
if os.path.exist(wk_dir + "/" + dir_name + "_%s_%dx%d_%d" % (a, resolution, resolution,n)):
os.mkdir(wk_dir + "/" + dir_name + "_%s_%dx%d_%d" % (a, resolution, resolution,m))
break
Your problem is a typo. It should be
os.path.exists(wk_dir + "/" + dir_name + "_%s_%dx%d_%d" % (a, resolution, resolution,n))
Instead of
os.path.exist(wk_dir + "/" + dir_name + "_%s_%dx%d_%d" % (a, resolution, resolution,n))
Note that os.path.exists
returns true if there's anything with the name passed as argument, be it file or directory.
To check if a file exists:
import os
if os.path.isfile("~/myfile"):
print("this file exists!")
else:
print("file not found!")
To check if a directory exists:
import os
if os.path.isdir("~/mydir"):
print("this directory exists!")
else:
print("directory not found!")