Trying to translate linux cmd to python script
Linux cmds:
chmod 777 file1 file1/tmp file2 file2/tmp file3 file3/tmp
I know of os.chmod(file, 0777)
but I'm not sure how to use it for the above line.
#!/usr/bin/python import os, sys, stat # Assuming /tmp/foo. txt exists, Set a file execute by the group. os. chmod("/tmp/foo.
In a nutshell, chmod 777 is the command you'll use within the Terminal to make a file or folder accessible to everyone. You should use it on rare occasions and switch back to a more restrictive set of permissions once you're done.
You should give permission 755 instead. That way, you as the file owner have full access to a certain file or directory, while everyone else can read and execute, but not make any modifications without your approval.
os.chmod
takes a single filename as argument, so you need to loop over the filenames and apply chmod
:
files = ['file1', 'file1/tmp', 'file2', 'file2/tmp', 'file3', 'file3/tmp']
for file in files:
os.chmod(file, 0o0777)
BTW i'm not sure why are you setting the permission bits to 777
-- this is asking for trouble. You should pick the permission bits as restrictive as possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With