Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 848
Xandy Avatar asked Mar 15 '18 20:03

Xandy


People also ask

How do I run chmod in Python?

#!/usr/bin/python import os, sys, stat # Assuming /tmp/foo. txt exists, Set a file execute by the group. os. chmod("/tmp/foo.

How do I use chmod 777 to file?

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.

What can I use instead of chmod 777?

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.


1 Answers

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.

like image 67
heemayl Avatar answered Oct 17 '22 00:10

heemayl