Forums

Folder created automatically on pythonanywhere source code directory

In my flask.cfg folder, I have the following setting:

MEDIA_ROOT = '/home/ujahdotpy/xy/xyz/project/static/img/images/'
MEDIA_URL = '/static/img/images/'

And my URL and Directory on my web tab as:

  /home/ujahdotpy/xyz/xyz/project/static/img/images/
 /static/img/images/

My problem is when I upload a new image file, a new 'images' folder is created and the newly uploaded image is stored in it making it inaccessible when I try to view it online.

How can I solve this?

do you mean the url is

/static/img/images/

and the directory is

/home/ujahdotpy/bulk/bulkieshare/project/static/img/images/

where is the new images folder created?

@Conrad, it is created inside the existing images folder so that I have something like this:

/home/ujahdotpy/bulk/bulkieshare/project/static/img/images/images/

I don't see a static file mapping setup on your webapp.

@conrad, @Dullong advise I remove it

if you want to serve static files the best way would be to setup a static file mapping.

please give the mapping url + directory, and sample image path on your file system, and the sample url endpoint that you are trying to access to see it the image online.

How to create a folder through of script?

r/engrish

I tried with os.mkdir() but it won't work.

What error are you getting? If you are getting a FileExistsError, you can safely ignore it:

try:
    os.mkdir(pathname)
except FileExistsError:
    pass

You can see errors from your Python code in the error log, which can be found in the web tab. You also have to remember to reload your web app whenever you make changes to code or static file mappings.

OSError: write error

Maybe you can try doing chmod +w command on one of the parent directories

Actually that probably won't work. I did some testing myself and got PermissionError instead of OSError

Actually my code snippet is like this:

dir_name = data["folder"]
if dir_name not in os.listdir(app.config["STATIC_PATH"]):
  os.chdir(app.config["STATIC_PATH"])
  os.mkdir(dir_name)

It's generally not a good idea to change the working directory when inside website code -- it just gives you one extra bit of global state to worry about it. It would also probably make things easier to debug if you print out the variables that specify the directory you're trying to create.

So I'd suggest reworking the code to be:

dir_name = data["folder"]
print("dir_name is {!r}".format(dir_name))
print("STATIC_PATH is {!r}".format(app.config["STATIC_PATH"]))
if dir_name not in os.listdir(app.config["STATIC_PATH"]):
    os.mkdir(os.path.join(app.config["STATIC_PATH"], dir_name))

The output from those print statements will appear in the website's server log, so you can check them out and see if they're what you expect them to be. If you're still confused, let us know what the variables' values are.

I changed but it still won't work, the same error. Where can I to see print() output?

In the server log, which you can find through the web tab.

There is no output for print(). SocketIO works well on pythonanywhere?

You could also try using sys.stderr.write() and find the output in the error log

@rjunior8 Are there any other error messages in our /var/log/rjunior8.pythonanywhere.com.error.log?

I figured out. The problem is that pythonanywhere doens't support websockets like SocketIO.

add_monitor.html

var socket = io.connect(document.location.origin + '/create_folder');

$(document).ready(function(){
  $('#create_dir').on('click', function(){
    $('#create_folder').modal('hide');
    socket.emit('/create/folder', {'folder' : $('#folder').val()});
  });
});

routes.py

@socketio.on("/create/folder", namespace="/create_folder")
def create_folder(data):
  try:
    dir_name = data["folder"]
    if dir_name not in os.listdir(app.config["STATIC_PATH"]):
      os.chdir(app.config["STATIC_PATH"])
      os.mkdir(dir_name)
      emit("created_folder", {"result" : "success"}, broadcast=False)
    else:
      emit("created_folder", {"result" : "failure"}, broadcast=False)
  except Exception as e:
    print("\n\n{}\nERROR ON LINE: {}\n".format(e, sys.exc_info()[-1].tb_lineno))
    emit("created_folder", {"result" : "failure"}, broadcast=False)

The create_folder() function is not called because the socket fails.

Ah. Ok. Glad you tracked it down.