简体   繁体   中英

Permissions Error and No such file or Directory

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM