【问题标题】:Python: Setting Multiple Attributes for a file (e.g. System, Hidden)Python:为文件设置多个属性(例如系统、隐藏)
【发布时间】:2015-04-13 23:30:24
【问题描述】:

使用 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)

这最终将只有 Hidden 属性。

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

【问题讨论】:

    标签: python attributes file-attributes bitflags


    【解决方案1】:

    好的,这是解决方案。我把它做成了通用的。

    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)
    

    【讨论】:

    猜你喜欢
    • 2013-11-06
    • 2011-08-12
    • 2015-01-27
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 2011-03-21
    相关资源
    最近更新 更多