首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Python中获取文件属性(隐藏、只读、系统、存档

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

Stack Overflow用户
提问于 2014-11-29 05:29:38
回答 3查看 9.7K关注 0票数 5

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

EN

回答 3

Stack Overflow用户

发布于 2015-10-31 01:41:41

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

代码语言:javascript
运行
复制
import win32con
import win32api
attrs = win32api.GetFileAttributes(filepath)
attrs & win32con.FILE_ATTRIBUTE_SYSTEM
attrs & win32con.FILE_ATTRIBUTE_HIDDEN
票数 6
EN

Stack Overflow用户

发布于 2014-11-29 05:45:11

您需要了解一下模块statos.stat

代码语言:javascript
运行
复制
 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)
票数 3
EN

Stack Overflow用户

发布于 2019-02-21 22:29:59

如果你使用的是python 3.4+,你可以使用pathlib stat方法。

代码语言:javascript
运行
复制
from pathlib import Path

print(Path(r"D:\temp\test.txt").stat())

输出:

代码语言:javascript
运行
复制
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
)

here is more information about os.stat_result

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27196143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档