Permission problem deleting file in python
I generate a file in the tmp
directory of my root server.
touch "sync.txt"
chmod 777 "sync.txt"
and I try to delete it with python like this
os.remove('/t开发者_如何学运维mp/sync.txt')
but I always get this error
exceptions.OSError: [Errno 1] Operation not permitted: '/tmp/sync.txt'
I thought this would be solved by settings the file permission to 777 but I still have this problem.
When I make
chmod 777 /tmp
instead I get another error:
exceptions.OSError: [Errno 2] No such file or directory: '/tmp/sync.txt'
However the file is deleted.
Any idea whats wrong? How can I fix that exception?
If /tmp
is marked sticky (chmod a+t
) then only the superuser or the owner of the directory or file within it will be able to delete it, regardless of what permissions the file has.
By default, everyone can write in /tmp
. You need to chown [python_running_user]:[its_group] /tmp/sync.txt
. chmod 777 /tmp/sync.txt
is not mandatory. 644
will be enough.
精彩评论