0
if not os.path.exists('/var/log/'):
    os.makedirs('/var/log/')
print(log_filepath)
os.chmod(log_filepath, stat.S_IWOTH)

f_log_in = open(log_filepath, "a")

Without the chmod command, it will throw an error saying permission denied for the f_log_in file open command.

    f_log_in = open(log_filepath, "a")
PermissionError: [Errno 13] Permission denied: '/var/log/s3_sync.log'

When I include the os.chmod command, it says:

   os.chmod(log_filepath, stat.S_IWOTH)
FileNotFoundError: [Errno 2] No such file or directory: '/var/log/s3_sync.log'

Are there any other ways of approaching this?

EDIT: THIS IS NOT A DUPLICATE, I DELETED THE OTHER ONE.

  • Will you please add the output of `ls -l /var/log` and `ls -l /var/log/s3_sync.log` to the question? –  Oct 25 '21 at 20:25
  • @user17242583 ls -l gives me a huge list. The second one does not exist. – winterlyrock Oct 25 '21 at 21:27
  • Oh sorry, I meant `ls -ld /var/log`. So I can see the rwx permission string for `/var/log`. –  Oct 25 '21 at 21:51

1 Answers1

0

The file indicated in log_filepath doesn't exist. Thus, you cannot simply open it, as you do with open. See this answer for more info, but you need w+ or a+ to also create the file.

The second error you get is because of exactly the message - the file doesn't exist, so you can't change the permissions to write to it.

Now, you might still have a problem if you are not executing the program as a user with sufficient permissions to access /var/log (or wherever log_filepath points to). You have to run as a user with sufficient permission - there is no way around that, either by running as a user with that permission or by changing the permissions of the directory itself so that the user you are executing as would have sufficient permission.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102