Python-Ref > System interaction > Manipulating files and directories > File permissions
 
 

<-^^->

File permissions

How to query and modify file permissions.
The following piece of code checks the permissions of files and directories.
Expand/Shrink
Prostředí:
.
|-- [dr-xr-xr-x beda     users   ]  empty
|-- [-r--r----- beda     users   ]  example.txt
|-- [-rw-r--r-- beda     users   ]  filesystem4-1.py
|-- [drwx------ beda     users   ]  something
|   `-- [-rw-r--r-- beda     users   ]  example.py
`-- [drwxrwxrwx beda     users   ]  test
    `-- [-rw-r--r-- beda     users   ]  test.txt
  1   import os
  2   
  3   for path in os.listdir( "./"):
  4       print path
  5       if os.access( path, os.R_OK): # is path readable?
  6           print "  - readable"
  7       else:
  8           print "  - unreadable"
  9       if os.access( path, os.W_OK): # is path writable?
 10           print "  - writable"
 11       else:
 12           print "  - read-only"
 13       if os.access( path, os.X_OK): # is path executable?
 14           print "  - executable"
 15       else:
 16           print "  - non-executable"
stdout:
filesystem4-1.py
  - readable
  - writable
  - non-executable
empty
  - readable
  - read-only
  - executable
example.txt
  - readable
  - read-only
  - non-executable
something
  - readable
  - writable
  - executable
test
  - readable
  - writable
  - executable
Doba běhu: 34.3 ms
If you need to test the ownership of files, you can use the code below.
Expand/Shrink
Prostředí:
.
|-- [beda     users   ]  empty
|-- [beda     users   ]  example.txt
|-- [beda     users   ]  filesystem4-2.py
|-- [beda     users   ]  something
|   `-- [beda     users   ]  example.py
`-- [beda     users   ]  test
    `-- [beda     users   ]  test.txt
  1   import os
  2   
  3   for path in os.listdir( "./"):
  4       stat = os.stat( path)
  5       # owner and group are given as ids, that is numbers
  6       print "%-20s owner=%-6d group=%-6d" % (path, stat.st_uid, stat.st_gid)
stdout:
filesystem4-2.py     owner=1006   group=100   
empty                owner=1006   group=100   
example.txt          owner=1006   group=100   
something            owner=1006   group=100   
test                 owner=1006   group=100   
Doba běhu: 33.7 ms
Changing file and directory permissions seems a little strange at first, but it is quite easy. The only challenge here is to remember the abbreviation like S_IRGRP.
Expand/Shrink
Prostředí:
.
|-- [dr-xr-xr-x]  empty
|-- [-r--r-----]  example.txt
|-- [-rw-r--r--]  filesystem4-3.py
|-- [drwx------]  something
|   `-- [-rw-r--r--]  example.py
`-- [drwxrwxrwx]  test
    `-- [-rw-r--r--]  test.txt
  1   import os
  2   import stat  # defines constants for chmod
  3   
  4   for path in os.listdir( "./"):
  5       if os.path.isdir( path):
  6           os.chmod( path, stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP)
  7           print "making directory rwxr-x---", path
  8       if os.path.isfile( path):
  9           os.chmod( path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP)
 10           print "making file      rw-r-----", path
stdout:
making file      rw-r----- filesystem4-3.py
making directory rwxr-x--- empty
making file      rw-r----- example.txt
making directory rwxr-x--- something
making directory rwxr-x--- test
Prostředí:
.
|-- [drwxr-x---]  empty
|-- [-rw-r-----]  example.txt
|-- [-rw-r-----]  filesystem4-3.py
|-- [drwxr-x---]  something
|   `-- [-rw-r--r--]  example.py
`-- [drwxr-x---]  test
    `-- [-rw-r--r--]  test.txt
Doba běhu: 33.4 ms