-1

Alright, I'm creating a Python project of mine using my two favorite modules. Flask and discord. Now thus to say, I'm not the best, but I'm encountering a new error when trying to create a new file when directory is empty of said name. Here's the error I'm getting:


2.168.0.26 - - [18/Jun/2021 21:30:42] "GET / HTTP/1.1" 404 -
Trying to open Mewniverse's folder
File not found error raised
File not accessible. creating new.
[2021-06-18 21:30:42,559] ERROR in app: Exception on /favicon.ico [GET]
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "<string>", line 53, in hello_world
OSError: [Errno 30] Read-only file system: '/Mewniverse'
192.168.0.26 - - [18/Jun/2021 21:30:42] "GET /favicon.ico HTTP/1.1" 500 -

Now here's my code(ignoring indentation and unfinished parts as it is heavily unfinished):


def hello_world(ID):
    print(f"Trying to open {ctx.author.name}'s folder")
    directory = f'/{ctx.author.name}'
    parent_dir = 'userFiles/templates'
    path = os.path.join(parent_dir, directory)

    if os.path.isfile(path):
        print ("File exist")
    else:
        print(f'File not found error raised')
        print("File not accessible. creating new.")
        f = open(path, "w")
        f.write("Now the file has more content!")
        f.close()
        print("Directory created and written in")

    return render_template(f'{ctx.author.name}.html')
        
    
app.run(host='0.0.0.0', port=8080)
            
        
await ctx.message.channel.send(f'Here you go {ctx.author.name}! Your site will be here: http://192.168.0.26:8080/{ctx.author.name}')

I've been stuck here for a few hours, so I'm hoping someone could help find a solution quickly. Thanks if possible.

Damoiskii
  • 1,328
  • 1
  • 5
  • 20
  • Is this line `directory = f'/{ctx.author.name}'` a filename or directory name? – Damoiskii Jun 19 '21 at 03:42
  • {ctx.author.name} is what, in discord.py, translates to the users name that used the command. So when I run $create, the bot will see what user sent said command, and basically fill that variable as {ctx.author.name}. so when I put it in there as the filename, its supposed to check to see if a file with the name of said user is created, and if not, create one. That being said, it does check for a file with the same name as my username when i run it. I hope that answers your question. – TutorialWarrior 9776 Jun 19 '21 at 03:57
  • Not sure if this is the solution but it looks like you can't create a new file because it's a read only directory. See if this post helps: https://stackoverflow.com/questions/63085356/oserror-errno-30-read-only-file-system-user-macos-catalina – Dimitar Pendurkov Jun 19 '21 at 02:55
  • I read it over, and it seems that their problem was touching root folders, though mine doesn't involve any. It also had a command in there to change folder per motions, but it doesn't look like the (correct type of console code)(?) For my system. Thanks for the suggestion though – TutorialWarrior 9776 Jun 19 '21 at 03:01
  • You may want to check out this [question](https://stackoverflow.com/questions/65524379/how-to-fix-read-only-file-system-error-python). See if what was suggested there helps. – Damoiskii Jun 19 '21 at 04:28
  • That's not a bad suggestion at all, the thing with that is though, its not with Python at all. Their running shell scripts to edit a file extension. I can't exactly be using batch files in this code due to what I'm programming with. I need to be able to use pure Python to check, add, and edit files without this error. Thanks though! – TutorialWarrior 9776 Jun 19 '21 at 04:58
  • 1
    How are you executing the script? Are you calling it directly, or via a service or something? Because when calling a script as a service, you need to give the **absolute** path to the files you want to work with. If your OS **thinks** that `path` is an absolute path, it will try to access something with very high privileges on the file tree, which might cause the problem. – itzFlubby Jun 19 '21 at 08:08
  • Yea i think thats it! I never did add the absolute path so thank u! – TutorialWarrior 9776 Jun 19 '21 at 10:18

1 Answers1

0

Answer from the comments:

How are you executing the script? Are you calling it directly, or via a service or something? Because when calling a script as a service, you need to give the absolute path to the files you want to work with. If your OS thinks that path is an absolute path, it will try to access something with very high privileges on the file tree, which might cause the problem. – itzFlubby

Its 5 in the morning and i need sleep. So i just copied the answer down for you all to understand this is the fix. Thanks a lot <3

General Grievance
  • 4,555
  • 31
  • 31
  • 45