Python—獲取文件基本信息


import os

def formatTime(longtime):

  '''格式化時間的函數'''

  import time

  return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(localtime))

def formatByte(number):

  '''格式化文件大小的函數'''

  for(scale.label) in [(1024*1024*1024,"GB"),(1024*1024,"MB"),(1024,"KB")]:

    if number>=scale:

      return  "%.2f %s" %(number*1.0/scale,lable)

    elif number ==1:

      return  "1字節"

    else:  #小於1字節

      byte = "%.2f" % (number or 0)

  return (byte[:-3]) if byte.endswith(".00") else byte) + "字節"

 

fileinfo = os.stat("mr.png")  #獲取文件的基本信息

print("文件完整路徑:",os.path.abspath("mr.png"))  #獲取文件的完整路徑

#輸出文件的基本信息

print("索引號:",fileinfo.st_ino)

print("設備名:",fileinfo.st_dev)

print("文件大小:",formatByte(fileinfo.st_size))

print("最后一次訪問時間:",formatTime(fileinfo.st_atime))

print("最后一次修改時間:",fileinfo.st_mtime)

print("最后一次狀態變化的時間:",fileinfo.st_ctime)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM