Forums

os.path.exists() throws FileNotFoundError although using absolut paths

Hi,

trying to deploy my first flask web app, so please bear with me ;) When I add a new object to my database via flask_admin it automatically triggers (via after_model_change( ) ) a function that checks if for each object a folder exists (where it will save some files latwer). If it does not, it should create it.

def create_folders():
names = get_newsletter_names()
for name in names:
    folder_exists(name)

def folder_exists(foldername):
if not os.path.exists('/home/n2kindle/N2Kindle/N2Kindle/main/files' + foldername):
    os.mkdir('/home/n2kindle/N2Kindle/N2Kindle/main/files' + foldername)
else:
    print(foldername + " folder already exists")

However, although I'm using absolute paths, I get the following error: File "/home/n2kindle/N2Kindle/N2Kindle/main/send.py", line 82, in folder_exists if not os.path.exists('/home/n2kindle/N2Kindle/N2Kindle/main/files' + foldername): FileNotFoundError: [Errno 2] No such file or directory: './main/files/test'

test here is the name field of the object I just added via flask admin.

Why does it look in './main/files/test' (relative path) when I only specified absolut paths? I assume that is where the problem is, right?

Help is much appreciated

Have your reloaded your web app since you made a change? You may be running old code.

you mean just reload in the browser or is there a special button in pythonanywhere to reload all files?

Ignore haha, found the big green button Working now. Thanks a lot ;)

great!

             / ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄\    
<<<<<<:>~  <   Yay!          |   
             \_________/