首页 > 解决方案 > 如何以编程方式阻止应用程序读取计算机文件?

问题描述

在像 Python 这样的脚本语言中,如何阻止应用程序读取计算机文件,尤其是敏感的系统级文件?

编辑:要清楚,我的意思是将该应用程序设置为无权访问计算机文件,即使在脚本运行完成后(设置权限)也是如此。

标签: pythonsecurityscriptingpermissions

解决方案


您应该以非特权用户身份运行您的 python 脚本并在验证访问权限后读取文件

# check readability of the path 
if os.access("file.txt", os.R_OK):  
    # open txt file as file 
    with open("file.tx") as file: 
        return file.read() 

推荐阅读