python3---对windows系统的文件夹与文件属性为隐藏、只读等。os.chdir、os.getcwd、win32api、win32con

python3—对windows系统的文件夹与文件隐藏
参考:python3.4对应下载https://www.jb51.net/softs/416131.html
设置文件夹与文件的属性参考:https://www.cnblogs.com/dcb3688/p/4608016.html
判断文件夹与文件是否隐藏参考:https://zhidao.baidu.com/question/136250336664582445.html

确认是否安装成功,如下:
C:\Python34>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32api
>>> import win32con
>>> import win32file
>>> 
windows的文件夹与文件属性值信息:
FILE_ATTRIBUTE_READONLY = 1 (0x1)  # 属性-隐藏
FILE_ATTRIBUTE_HIDDEN = 2 (0x2)   # 属性-隐藏
FILE_ATTRIBUTE_SYSTEM = 4 (0x4)   # 属性-系统文件
FILE_ATTRIBUTE_DIRECTORY = 16 (0x10)
FILE_ATTRIBUTE_ARCHIVE = 32 (0x20)
FILE_ATTRIBUTE_NORMAL = 128 (0x80)   # 属性-正常
FILE_ATTRIBUTE_TEMPORARY = 256 (0x100)
FILE_ATTRIBUTE_SPARSE_FILE = 512 (0x200)
FILE_ATTRIBUTE_REPARSE_POINT = 1024 (0x400)
FILE_ATTRIBUTE_COMPRESSED = 2048 (0x800)
FILE_ATTRIBUTE_OFFLINE = 4096 (0x1000)
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192 (0x2000)
FILE_ATTRIBUTE_ENCRYPTED = 16384 (0x4000)
测试的文件夹与文件:
C:\test>dir
 驱动器 C 中的卷没有标签。
 卷的序列号是 1C41-4CCA

 C:\test 的目录

2018-06-13  10:50    <DIR>          .
2018-06-13  10:50    <DIR>          ..
2017-12-14  11:24            56,570 11.PNG
2017-12-14  14:17             1,498 3.js
2018-06-13  10:50    <DIR>          work2
               2 个文件         58,068 字节
               3 个目录 33,063,419,904 可用字节

C:\test>


C:\test>cd work

C:\test\work>dir 
 驱动器 C 中的卷没有标签。
 卷的序列号是 1C41-4CCA

 C:\test\work 的目录

2016-08-30  14:30             7,419 20160830.wvs
2017-02-15  10:37            30,208 555.xls
2016-05-26  15:45           169,984 bookmarks4.html
2009-07-14  13:32           780,831 orderDetail_export_2017-02-150.png
2017-07-20  20:27               816 orderExchangeDetail.zip
2018-06-11  09:27                 0 see holl.wmv
2016-09-08  12:11               159 Test.java
2016-09-08  11:56                 0 test.txt
2018-06-11  09:27                 0 大大.rvmb
2018-06-11  09:28                 0 蜂鸟.mp4
              10 个文件        989,417 字节
               0 个目录 33,063,419,904 可用字节

C:\test\work>
1、在python3.4进行调试windows下目标目录的文件隐藏属性与正常属性
C:\Python34>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32api
>>> import win32con
>>> import win32file
>>> import os
>>> os.getcwd()
'C:\\Python34'
>>> os.chdir("C:/test/work/")#切换目录
>>> os.getcwd()
'C:\\test\\work'
>>> attr = win32api.GetFileAttributes("555.xls")#获取属性值(正常)
>>> print(attr)
32
>>> attr1 = win32api.GetFileAttributes("1231.jpg")
>>> print(attr1)
32
>>>#隐藏该目录下的文件
>>> win32api.SetFileAttributes('1231.jpg',win32con.FILE_ATTRIBUTE_HIDDEN)#设置隐藏属性值
>>> 
>>> os.chdir("C:/test/work/")
>>> os.getcwd()
'C:\\test\\work'
>>> attr3 = win32api.GetFileAttributes("1231.jpg")
>>> print(attr3)
2
>>> 
#恢复文件1231.jpg的正常属性
>>> 
>>> os.chdir("C:/test/work")
>>> os.getcwd()
'C:\\test\\work'
>>> win32api.SetFileAttributes('1231.jpg',win32con.FILE_ATTRIBUTE_ARCHIVE)
>>> attr6 = win32api.GetFileAttributes("1231.jpg")
>>> print(attr6)
32
>>> 
2、windows系统下设置目标目录的文件夹隐藏与正常属性
>>> win32api.SetFileAttributes('work',win32con.FILE_ATTRIBUTE_HIDDEN)
>>> 
>>> os.chdir("C:/test")
>>> os.getcwd()
'C:\\test'
>>> attr4 = win32api.GetFileAttributes("work")
>>> print(attr4)
18
>>> 
#文件夹work恢复正常属性
>>> os.getcwd()
'C:\\test'
>>> win32api.SetFileAttributes('work',win32con.FILE_ATTRIBUTE_ARCHIVE)
>>> attr5 = win32api.GetFileAttributes("work")
>>> print(attr5)
48
>>> 
3、windows系统下判断文件是否隐藏:
参考:https://zhidao.baidu.com/question/136250336664582445.html
https://www.jb51.net/article/48312.htm

>>> os.getcwd()
'C:\\test\\work'
>>> attr7 = win32api.GetFileAttributes("1231.jpg")#未隐藏
>>> print(attr7)
32
>>> win32api.SetFileAttributes('1231.jpg',win32con.FILE_ATTRIBUTE_HIDDEN)#设置为隐藏
>>> attr8 = win32api.GetFileAttributes("1231.jpg")
>>> print(attr8)
2
>>> if attr8 & win32con.FILE_ATTRIBUTE_HIDDEN:#判断是否隐藏
...     print("True")
... else:
...     print("False")
... 
True
>>> 

猜你喜欢

转载自blog.csdn.net/xwbk12/article/details/80676701