我正在嘗試使用 Python 在 Unix 系統中設定檔案權限。
我的代碼大致如下所示:
os.chmod('/foo/bar.txt', 0o644, follow_symlinks=False)
該代碼在使用 Python 3.10.8 的 macOS Monterey 上運行良好。
奇怪的是,在帶有 Debian 11 和 Python 3.9.2 的 Linux VM 中它失敗了:
NotImplementedError: chmod: follow_symlinks unavailable on this platform
但是,當我嘗試os.supports_follow_symlinks時它不會拋出例外。
有誰明白為什么會這樣?
uj5u.com熱心網友回復:
chmod(2) 的 Linux 手冊頁:
* chmod() changes the mode of the file specified whose pathname is given in pathname, which is dereferenced if it is a symbolic link.
和
AT_SYMLINK_NOFOLLOW If pathname is a symbolic link, do not dereference it: instead operate on the link itself. This flag is not currently implemented.
和
ENOTSUP (fchmodat()) flags specified AT_SYMLINK_NOFOLLOW, which is not supported.
所以linux不支持。MacOS 有——但以一種違反直覺的方式。
os.supports_follow_symlinks是一組支持不跟隨符號鏈接的 Python 函式,它不會拋出任何例外,因為它不是函式。在 Linux 上,該集合不包含chmod(2).
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/537362.html
上一篇:SharePointRESTAPI:當串列名稱包含空格時無法通過getByTitle獲取串列
下一篇:Unix以編程方式重定向命令
