Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中获取文件属性(隐藏、只读、系统、存档)_Python_Attributes_Operating System_Chmod_Stat - Fatal编程技术网

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

在Python中获取文件属性(隐藏、只读、系统、存档),python,attributes,operating-system,chmod,stat,Python,Attributes,Operating System,Chmod,Stat,刚开始学习Python。如何在Python中获取文件属性的状态?我知道os.chmod(fullname,stat.S_IWRITE)delete readonly属性,但我如何在不更改它的情况下获取状态?我需要获得“隐藏”、“系统”、“只读”、“存档”的所有属性。您需要查看模块stat和os.stat os.stat(path) Perform the equivalent of a stat() system call on the given path. (This function

刚开始学习Python。如何在Python中获取文件属性的状态?我知道
os.chmod(fullname,stat.S_IWRITE)
delete readonly属性,但我如何在不更改它的情况下获取状态?我需要获得
“隐藏”
“系统”
“只读”
“存档”
的所有属性。您需要查看模块
stat
os.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)

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

import win32con
import win32api
attrs = win32api.GetFileAttributes(filepath)
attrs & win32con.FILE_ATTRIBUTE_SYSTEM
attrs & win32con.FILE_ATTRIBUTE_HIDDEN

如果您使用的是Python3.4+,则可以使用方法

输出:

os.stat_result(
    st_mode=33206, 
    st_ino=204632308068721491, 
    st_dev=67555953, 
    st_nlink=1, 
    st_uid=0, 
    st_gid=0, 
    st_size=4, 
    st_atime=1550757968, 
    st_mtime=1550757968, 
    st_ctime=1550757951
)

哪种操作系统,例如linux上的隐藏文件是任何名称的第一个字符为
的文件,但在windows上,我认为它是一个文件属性。
attrs&
部分做什么?FileAttributes是flags integer,因此需要检查每个位,每个位代表一个布尔值。隐藏属性在哪里?
os.stat_result(
    st_mode=33206, 
    st_ino=204632308068721491, 
    st_dev=67555953, 
    st_nlink=1, 
    st_uid=0, 
    st_gid=0, 
    st_size=4, 
    st_atime=1550757968, 
    st_mtime=1550757968, 
    st_ctime=1550757951
)