Search code examples
pythonlinuxchmod

chmod 777 to python script


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.


Solution

  • 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.