Python instance 26 [query attributes of modified files]

Source: Internet
Author: User

Python to obtain files

Common attributes: Size, modification time, etc;

Remove the readonly attribute of the file;

For Windows files, you can obtain the archive and version information of the files;

 

Code:

Import OS
Def Testfileattributes ():
# This function is platform indepedent.
Statinfo = OS. Stat ( " C: \ python26 \ python.exe " )
Print Statinfo. st_size
Print Statinfo. st_atime
Print Statinfo. st_mtime
Print Statinfo. st_ctime
# Statinfo also include other Linux specific information.
# Print statinfo

Testfileattributes ()
# 27136
# 1299820024.28
# 1228458748.0
# 1228458748.0

Import Stat
Def Testforchangetowrite (PATH ):
# This is platform indepedent.
If   Not OS. Access (path, OS. w_ OK ):
OS. chmod (path, stat. s_iwrite)

Testforchangetowrite ( " C: \ python26 \ python.exe " )

# ######################################## #########################

Import WIN32API, win32con
Def Testwinfileattributesifreadonly ():
# This is just for Windows.
Fattrs = WIN32API. getfileattributes ( " C: \ python26 \ python.exe " )
# Print fattrs
Print Bool (fattrs & Win32con. file_attribute_readonly)
 
Testwinfileattributesifreadonly ()
# False

Def Testwinfileattributesifhidden ():
# This is just for Windows.
Fattrs = WIN32API. getfileattributes ( " C: \ python26 \ python.exe " )
# Print fattrs
Print Bool (fattrs & Win32con. file_attribute_hidden)
 
Testwinfileattributesifhidden ()
# False

From WIN32API Import Getfileversioninfo, loword, hiword
Def Get_version_number (filename ):
# This is just for Windows.
Info = Getfileversioninfo (filename, " \\ " )
# Print info
MS = Info [ ' Fileversionms ' ]
Ls = Info [ ' Fileversionls ' ]
Print Hiword, loword (MS), hiword (LS), loword (LS)

Get_version_number ( " C: \ Program Files \ 7-zip \ 7z.exe " )
# 9 20 0 0

 

Complete!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.