Forums

Random problems saving files

I have this problem on PythonAnywhere, that I've experienced across scripts.

If I try to save a file in a folder, sometimes the script won't save, seemingly at random, no error message. It just doesn't show up in the folder.

An example of writing a csv file:

    uploadfile = "/home/xxxx/yyyy/uploads/" + file + '_upload.csv'

    with open(uploadfile, mode='w') as uploadFile:

Am I doing something wrong or is it on your side?

This happens the most with tasks.

If I run it manually one or more times, then I usually get it to save

That sounds strange. What is the value of the file variable in that code? Might it have odd stuff in it like ".."?

It's just a csv file with text and numbers.

No, I mean the variable file that you are inserting into a string to make uploadfile, not the contents that you are trying to write to the file.

Ok, no that's just standard text and static, from a function call.

What can I do to debug this?

Add some logging to your code so you can see what it's doing. My guess is that you have a code path that completely skips the file writing somehow. If you add logging and use it to identify exactly what your code is doing, you should be able to see what's happening.

What would you recommend for logging?

Simple print() will do, then you should look for the output in your webapp's log files.