Python:为一个文件设置多个属性(例如System、Hidden)

2024-09-30 05:31:21 发布

您现在位置:Python中文网/ 问答频道 /正文

使用Python文件设置多个属性的方式是什么?在

例如,我想将文件的属性设置为System,Hidden。在

我可以使用下面这样的方法,但它只设置一个属性,并覆盖以前的写入:

import win32con, win32api, os

filename = "some file name"

win32api.SetFileAttributes(filename,win32con.FILE_ATTRIBUTE_SYSTEM)
win32api.SetFileAttributes(filename,win32con.FILE_ATTRIBUTE_HIDDEN)

这将以隐藏属性结束。在

如何同时设置这两个属性?谢谢。在


Tags: 文件方法import属性os方式attributesome
1条回答
网友
1楼 · 发布于 2024-09-30 05:31:21

好的,这是解决办法。我把它做成通用的。在

import win32api
## If need to install pywin if not already to get win32api

## Define constants for Windows file attributes
FILE_ATTRIBUTE_READONLY = 0x01
FILE_ATTRIBUTE_HIDDEN = 0x02
FILE_ATTRIBUTE_SYSTEM = 0x04
FILE_ATTRIBUTE_DIRECTORY = 0x10
FILE_ATTRIBUTE_ARCHIVE = 0x20
FILE_ATTRIBUTE_NORMAL = 0x80
FILE_ATTRIBUTE_TEMPORARY = 0x0100

## Combine all the attributes you want using bitwise-Or (using the pipe symbol)
Attribute = FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM

filename="Your-filename-goes-here"

win32api.SetFileAttributes(filename,Attribute)

## Check that the attribute is set.
## You can also right click on the file in windows explorer and 
## look under Details tab.

print win32api.GetFileAttributes(filename)

相关问题 更多 >

    热门问题