Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

permission change of files in python

Tags:

python

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
like image 703
vrbilgi Avatar asked Aug 29 '11 09:08

vrbilgi


People also ask

How do I give permission to a folder in Python?

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 .

How do I check permissions on a file in Python?

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.

What is the meaning of chmod 777?

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.


2 Answers

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
like image 152
Dietrich Epp Avatar answered Sep 20 '22 05:09

Dietrich Epp


You can instead use an OS specific function call as follows:

os.system('chmod 777 -R *')
like image 27
user3262160 Avatar answered Sep 17 '22 05:09

user3262160