Member details
- Joined
- Aug 3, 2010
- Messages
- 1,283
What is the best way to read a files owner and permissions, create a new file then apply those permissions without using the subprocess method or command line icacls etc.
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
os and shutil. In order to read file permissions and ownership, you should use the following code:
import os import stat file_path = 'yourfile.txt' # get file permissions file_info = os.stat(file_path) file_permissions = stat.S_IMODE(file_info.st_mode) # get owner id owner_id = file_info.st_uid
st_mode contains information about file permissions, which you get using stat.S_IMODE. st_uid attribute is for user id who owns the file. To create a new file via Python, you can use the open function:
new_file_path = 'new_file.txt' # create a new file open(new_file_path, 'a').close
os.chmod method and ownership by os.chown method:
# applying permissions os.chmod(new_file_path, file_permissions) # applying onwership os.chown(new_file_path, owner_id, -1)
os.chmod doesn't support all permissions and os.chown function is not available in Python for Windows. In case you are working on Windows, you have to approach it differently, perhaps by using libraries that support Windows ACLs, like pywin32 or win32security as working with file permissions on Windows is more complicated due to the Access Control Lists (ACLs).os.chown function in Python, but the error happens because Windows does not support the chown attribute in the os module. The os.chown method is specific to Unix-like operating systems (e.g., Linux, macOS), and it doesn’t exist on Windows, as Windows handles file ownership differently.chown.icacls Command in Subprocessicacls command to modify ownership of a file or folder.import subprocess def take_ownership(file_path, owner): try: subprocess.run(["icacls", file_path, "/setowner", owner], check=True) print(f"Ownership of {file_path} changed to {owner}") except subprocess.CalledProcessError as e: print(f"Error taking ownership of {file_path}: {e}") # Usage: file_path = "C:\\Temp\\example_file.txt" owner = "YOUR-PC\\YourUsername" # Replace with your user take_ownership(file_path, owner)
icacls to set the ownership of the file. Be sure to replace "YOUR-PC\\YourUsername" with your actual user account in Windows.pywin32 for File Ownership (Advanced)import win32security import ntsecuritycon as con def set_owner(file_path, new_owner): # Lookup the user SID sd = win32security.GetFileSecurity(file_path, win32security.OWNER_SECURITY_INFORMATION) sid, _ = win32security.LookupAccountName(None, new_owner) # Set the new owner sd.SetSecurityDescriptorOwner(sid, 0) win32security.SetFileSecurity(file_path, win32security.OWNER_SECURITY_INFORMATION, sd) print(f"Ownership of {file_path} changed to {new_owner}") # Usage: file_path = "C:\\Temp\\example_file.txt" new_owner = "YOUR-PC\\YourUsername" # Replace with your Windows username set_owner(file_path, new_owner)
pywin32 package. Install it via:sh pip install pywin32
os.chmod method, which works on both Unix and Windows to set read/write access.import os file_path = "C:\\Temp\\example.txt" os.chmod(file_path, 0o777) # Grants full read/write/execute permissions
chmod does not fully account for ACL-based permissions.win32security module or third-party packages like pyACL.