简体   繁体   English

使用python检查ntfs文件系统中的写入权限

[英]Using python to check write permission in a ntfs file system

I need to write into several files that usually needs higher level permission than a regular user. 我需要写入几个通常需要比普通用户更高级别权限的文件。 Before doing anything, I would like the check if the current user can write in all of them. 在做任何事情之前,我想检查一下当前用户是否可以全部写入。 If so, proceed to writing. 如果是这样,请继续进行写作。 Otherwise warns the user and asks to check first the user level permission. 否则,警告用户并要求首先检查用户级别的权限。

In nix systems, like Linux and Mac OS, I can rely on os.access , but it seems that simply does not work on Windows. 在Linux和Mac OS等nix系统中,我可以依赖os.access ,但似乎根本无法在Windows上运行。 It tells that the file always have write permission, but proceeding to the writing I stumble in an unexpected exception of PermissionError - Permission denied. 它告诉该文件始终具有写许可权,但是继续进行写作时,我偶然遇到了PermissionError-许可被拒绝的意外异常。 I saw the StackOverflow link pointing to the win32security ( Checking folder/file ntfs permissions using python ). 我看到StackOverflow链接指向win32security( 使用python检查文件夹/文件ntfs权限 )。 But the topic is 10 years old, and checking for library today, it doesn't seems that library is currently supported or mantained. 但是该主题已有10年历史了,今天检查图书馆,似乎图书馆目前不受支持或维护。

Snippet to check permission: 片段以检查权限:

import os

if os.name == 'posix':
    file_to_write = '/etc/hosts'
else:
    file_to_write = 'C:\\Windows\\System32\\drivers\\etc\\hosts'

if os.access(file_to_write, os.W_OK):
    print("I can write in!")
else:
    print("Can't write. No permission.")

Then, the code to write in the file: 然后,将代码写入文件:

import os

if os.name == 'posix':
    file_to_write = '/etc/hosts'
else:
    file_to_write = 'C:\\Windows\\System32\\drivers\\etc\\hosts'

file_resource = open(file_to_write, 'a')
file_resource.write("\ntest")
file_resource.close()

Testing my snippets, using python3, in any operational system, or even testing any file in the system (not necessarily the hosts file in my example), python shall tells previously if the file can be written by the current user. 在任何操作系统上使用python3测试我的代码片段,或者甚至测试系统中的任何文件(在我的示例中不一定是hosts文件),python都应事先告知该文件是否可由当前用户编写。 If so, then the second snippet must not return an exception of PermissionError - Permission denied, and the target file shall have a word "test" in the file's last line. 如果是这样,则第二个片段不得返回PermissionError的异常-权限被拒绝,并且目标文件的最后一行应带有单词“ test”。 Otherwise, I won't even try the second snippet. 否则,我什至不会尝试第二个片段。 In Windows environment, python dumbly tells that any existing file have write permission. 在Windows环境中,python愚蠢地告知任何现有文件都具有写权限。

Maybe use a with statement that automatically closes. 也许使用with语句自动关闭。 'a+' will create the file if it does not already exist and append the file. 'a+'文件尚不存在,则会创建该文件并追加文件。 For reading and writing you can use 'r+' : 对于读写,您可以使用'r+'

import os

if os.name == 'posix':
    file_to_write = '/etc/hosts'
else:
    file_to_write = 'C:\\Windows\\System32\\drivers\\etc\\hosts'

with open('file_to_write', 'a+') as file_resource: 
    file_resource.write("\ntest") # .close() is automatically called 

If this does not work the permission error will be because you are trying to write to a path that needs a user privilege. 如果这不起作用,则权限错误将是因为您试图写入需要用户特权的路径。 If that is the case consider using an alternative of the chmod on a Linux machine. 如果是这种情况,请考虑在Linux计算机上使用chmod的替代方法。 In windows you can change the settings by right clicking the file. 在Windows中,您可以通过右键单击文件来更改设置。 Navigate to the properties settings, then the security settings to allow 'write' 导航到属性设置,然后导航到安全设置以允许“写入”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM