在 Python 中获取文件属性(隐藏、只读、系统、存档)

Evg*_*but 5 python attributes operating-system chmod stat

刚开始学Python。如何在 Python 中获取文件属性的状态?我知道os.chmod(fullname, stat.S_IWRITE)删除只读属性,但是如何在不更改它的情况下获得状态?我需要获得"hidden", "system", "readonly", 的所有属性"archive"

loo*_*ngz 6

您可以像这样直接使用 Windows API

import win32con
import win32api
attrs = win32api.GetFileAttributes(filepath)
attrs & win32con.FILE_ATTRIBUTE_SYSTEM
attrs & win32con.FILE_ATTRIBUTE_HIDDEN
Run Code Online (Sandbox Code Playgroud)


Hac*_*lic 5

你需要看一下模块statos.stat

 os.stat(path)

Perform the equivalent of a stat() system call on the given path. (This function follows symlinks; to stat a symlink use lstat().)

The return value is an object whose attributes correspond to the members of the stat structure, namely:

    st_mode - protection bits,
    st_ino - inode number,
    st_dev - device,
    st_nlink - number of hard links,
    st_uid - user id of owner,
    st_gid - group id of owner,
    st_size - size of file, in bytes,
    st_atime - time of most recent access,
    st_mtime - time of most recent content modification,
    st_ctime - platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows)
Run Code Online (Sandbox Code Playgroud)

  • 隐藏属性在哪里? (2认同)