1

I'm trying to remove file permissions in python. I am aware that the mode to do so is "000." However I'm seeing the removal of file permissions be done with flags as well such as "stat.S_IRWXO." Can anyone explain what I'm doing wrong

import os
import stat

file_path = 'random file'

os.chmod(file_path, stat.S_IRWXO)

My attempt with the "000" mode:

import os
import stat

file_path = "C:\Script\poop.txt"

os.chmod(file_path, 000)

EDIT

Using subprocesses, I was able to resolve the problem. I have not read the full documentation to know if chmod is not fully compatible with Windows, but it seems like it is at the very least, severely limited. Below is the code to use Window's "icacls" command to set permission. This is much more efficient.

import subprocess

file_path = r'C:\Script\poop.txt'

subprocess.check_output(['icacls.exe',file_path,'/deny','everyone:(f)'],stderr=subprocess.STDOUT)

SOURCES

calling windows' icacls from python

https://docs.python.org/2/library/os.html#os.chmod

gf807
  • 81
  • 1
  • 2
  • 7
  • have you tried to use `os.chmod(file_path, 000)` ? – Abe Mar 08 '18 at 14:16
  • Yes, it didn't work. I'm trying to modify a simple text file, if that helps – gf807 Mar 08 '18 at 14:27
  • Windwos doesn't have ``chmod``. – Psytho Mar 08 '18 at 14:37
  • @Psytho are there any other methods then that I can use to do this? – gf807 Mar 08 '18 at 14:41
  • 1
    Strangely enough it is possible to use ``os.chmod`` on WIndows (``chmod`` in terminal throws an error). YOu have to use double ``\\`` in the file path. – Psytho Mar 08 '18 at 14:42
  • Possible duplicate of [Setting folder permissions in Windows using Python](https://stackoverflow.com/questions/12168110/setting-folder-permissions-in-windows-using-python) – kabanus Mar 08 '18 at 15:01
  • @kabanus technically. I will put my solution in my original post – gf807 Mar 08 '18 at 15:11
  • Better to post an answer of your own, if the dupe doesn't fit. This will benefit future readers. You can accept it in a day. – kabanus Mar 08 '18 at 15:18
  • Side note: file permissions are usually written in octal (base 8). While it makes no difference for `000`, in general mode value in a program must be written like `0o644` or `0o22`. – VPfB Mar 08 '18 at 17:03

3 Answers3

3

This string:

file_path = "C:\Script\poop.txt"

is un-escaped. Thus the path becomes something like "C:Scriptpoop.txt". Use a raw string:

 file_path = r"C:\Script\poop.txt"

or use \\ instead of \.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • 1
    Also, "Scriptpoop" is an adequate name for temporary files created by a script and not cleaned. – kabanus Mar 08 '18 at 14:47
1

You can try to use subprocess module as an general solution:

import subprocess

file_path = 'file.txt'
subprocess.call(['chmod', '000', file_path])

terminal output ls -la:

-r--r--r-- 1 kernel 197121     0 Mar  8 10:29 file.txt
Abe
  • 1,357
  • 13
  • 31
  • The OP has a Windows machine. Or at least it looks that way. – Psytho Mar 08 '18 at 14:38
  • this code was tested in windows machine with git-bash installed. Try to [see the docs](https://docs.python.org/2/library/os.html#os.chmod) about windows support – Abe Mar 08 '18 at 14:41
1

On ms-windows, you can only use os.chmod to set and remove the read-only bit. All others bits are ignored.

Basically, file permissions work differently on ms-windows than on POSIX operating systems. You will have to modify Access Control Lists using win32 API calls. To do that from within Python, you will need pywin32.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94