Xandy
Xandy

Reputation: 175

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.

Upvotes: 10

Views: 32841

Answers (3)

edesz
edesz

Reputation: 12406

If you want to use pathlib, you can use .chmod() from that library

from pathlib import Path

files = ['file1', 'file1/tmp', 'file2', 'file2/tmp', 'file3', 'file3/tmp']
for file in files:
    Path(file).chmod(0o0777)

Upvotes: 1

benhorgen
benhorgen

Reputation: 1955

chmod 777 will get the job done, but it's more permissions than you likely want to give that script.

It's better to limit the privileges to execution.

I suggest: chmod +x myPthonFile.py

Upvotes: -1

heemayl
heemayl

Reputation: 42017

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.

Upvotes: 20

Related Questions