示例:
#检查文件是否存在 if os.path.exists("file.txt"): #使用open()函数读取文件 with open("file.txt", "r") as f: content = f.read() print(content)
#更改文件所有权和权限 os.chown("file.txt", 1001, 1001) os.chmod("file.txt", 0o777)
#使用sudo权限打开文件 sudo python app.py with open("file.txt", "r") as f: content = f.read() print(content)