Forums

writing in a file from root directory

Hey! I'm liturally trying the simplest thing ever. I have a python script that does nothing but add a line to a text file (log.txt) in the same folder as this script:

logFile = open("log.txt", "a")
logFile.write("added 1 line\n")
logFile.close()

Lovely, isn't it? Now, i want to automate this, so i setup a new task that runs this file once every 24 hours. Now, this script is in a folder under /home/username/foldername. When I try to run it from this directory "foldername", it works fine, but if i try it from root using python3.6 /home/username/foldername/script.py, it gives me this error:

Traceback (most recent call last): File "/home/username/foldername/script.py", line 1, in <module> logFile = open("log.txt", "a") PermissionError: [Errno 13] Permission denied: 'log.txt'

It seems like i do not have the permissions I need. Why does this happen, and how do I fix it? Thank you for helping!

Use absolute path in place of a relative one.