Having a script running on local debug server fine.
if not os.path:
os.mkdir(os.path.normpath(f'media/{request.user.id}/'))
file_path = os.path.normpath(f'media/{request.user.id}/' file_name '.csv')
On Heroku I get an error. If I do not create directory "media" and do not deploy it to Heroku, I get error 'media/1/' doesn't exist. What is wrong, why script can't create directory?
CodePudding user response:
The immediate issue is that your if
condition is against the os.path
module itself:
if not os.path:
os.path
is part of the standard library so if os.path
should always be True
as long as os
or os.path
has been imported. And if it ever did return False
(e.g. if you have a broken or stripped down Python environment) you wouldn't be able to use it in the if
block.
I suspect you want something more like this, using os.path.exists()
:
user_media_folder = os.path.normpath(f'media/{request.user.id}/')
if not os.path.exists(user_media_folder):
# ^^^^^^^^^^^^^^^^^^^^^^^^^^
os.mkdir(user_media_folder)
file_path = os.path.normpath(user_media_folder file_name '.csv')
In any case, assuming you don't want those files to disappear you won't be able to use the local filesystem anyway. It is ephemeral: any change you make to it will be lost whenever your dyno restarts. This happens frequently (at least once per day) and unpredictably.
You'll have to store uploads somewhere else, e.g. on Amazon S3 or Azure Blob Storage. See Django Heroku S3, for example.