I want to change the file permission for all the files from my current directory tree. I am trying to open each directory and open the files and change the permission using os.chmod()
, But getting an error.
import os
import stat
for files in os.walk('.'):
os.chmod(files,stat.S_IXGRP)
The error I get is:
File "delhis.py", line 4, in ? os.chmod(files,stat.S_IXGRP) TypeError: coercing to Unicode: need string or buffer, tuple found
chmod(path, 0444) is the Python command for changing file permissions in Python 2. x. For a combined Python 2 and Python 3 solution, change 0444 to 0o444 . You could always use Python to call the chmod command using subprocess .
os. stat is the right way to get more general info about a file, including permissions per user, group, and others. The st_mode attribute of the object that os. stat returns has the permission bits for the file.
Setting 777 permissions to a file or directory means that it will be readable, writable and executable by all users and may pose a huge security risk.
You are using os.walk
incorrectly.
for dirpath, dirnames, filenames in os.walk('.'):
for filename in filenames:
path = os.path.join(dirpath, filename)
os.chmod(path, 0o777) # for example
You can instead use an OS specific function call as follows:
os.system('chmod 777 -R *')
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