码迷,mamicode.com
首页 > 编程语言 > 详细

python获取文件属性

时间:2018-03-01 14:55:01      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:os模块   linux   linu   specific   获取文件   atime   bool   log   win   

提供三段代码

1、os模块

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

2、调用win32api和win32con

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

3、win32api下的GetFileVersionInfo

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 (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls)
get_version_number("C:\\Program Files\\7-Zip\\7z.exe")

 

python获取文件属性

标签:os模块   linux   linu   specific   获取文件   atime   bool   log   win   

原文地址:https://www.cnblogs.com/lcamry/p/8488055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!