【问题标题】:PermissionError: Python权限错误:Python
【发布时间】:2020-08-18 10:42:42
【问题描述】:

我想知道是否有任何方法可以获得 sudo 访问权限。这仅适用于 Linux。

# get machine's username 
userMachine = getpass.getuser()

# education > school's name > year
makeSchoolPath = "/home/userMachine/Education/schoolInput/str(yearCourse)" 
os.makedirs(makeSchoolPath)

# last path, but also creates a leaf directory for each subject
dict2 = {}
for key, value in dict.items():
    dict2 ['path' + value] = "/home/userMachine/Education/schoolInput/str(yearCourse/value"
    for value2 in dict2.values():
        os.mkdir(value2) 

我收到错误消息:“发生异常:PermissionError [Errno 13] 权限被拒绝:'/home/userMachine'”。我读到我应该写

chown -R user-id:group-id /path/to/the/directory

但我不想搞砸任何事情,而且我有点害怕,因为这意味着“更换所有者”。

感谢您阅读好人:)

【问题讨论】:

    标签: python permissions


    【解决方案1】:

    如果您使用的是 Linux,那么只需使用 sudo python <your file name.py> 运行您的代码。

    【讨论】:

    • 我想在代码里面做。我希望它能够在每台机器上自动运行。这可能吗?
    【解决方案2】:

    我很确定如果你输入“sudo python file.py”,你的代码应该有 sudo perms。告诉我这是否有效。

    【讨论】:

    • 有什么方法可以在代码中实现吗?
    • 我找到了这个解决方案。 (请注意我可能有引号)os.system(‘echo %s | sudo -S %s’ % (password, command))
    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2012-03-29
    • 2023-03-03
    • 2018-07-17
    • 2018-02-25
    • 2017-07-07
    • 2021-01-19
    • 2017-12-26
    相关资源
    最近更新 更多