1. 程式人生 > >python3獲取指定目錄內容的詳細信息

python3獲取指定目錄內容的詳細信息

logs python3 技術分享 1.3 join getc creat rsize 命令

不同平臺獲取指定目錄內容的詳細信息命令各不相同:

Linux中可以通過ls -al獲取獲取

技術分享

windows中可以通過dir命令獲取

技術分享

下面是我寫的一個通用獲取目錄內容詳細信息的python3腳本:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


import os, time, sys
from os.path import join, getsize


def ListDir(dir_data):
    ‘‘‘
    :param dir_data: 指定獲取內容的目錄
    每次返回目錄下的一個文件或文件夾(生成器練習)
    
‘‘‘ list_dir = os.listdir(dir_data) while True: try: list_obj = list_dir.pop() yield dir_data, list_obj except IndexError: return def GetDirSize(dir): ‘‘‘ :param dir: 指定獲取大小的目錄 獲取目錄大小 ‘‘‘ size = 0 for root, dirs, files in
os.walk(dir): size += sum([getsize(join(root, name)) for name in files]) return size def getFileInfo(dir_data): ‘‘‘ :param dir_data: 調用ListDir函數是所需參數 獲取目錄內的每個文件或目錄的屬性和大小並打印 ‘‘‘ info = {} for root, obj in ListDir(dir_data): dir_obj = %s/%s %(root, obj)
if os.path.isfile(dir_obj): #文件處理 ‘‘‘info為字典格式,方便返回調用,此腳本只是輸出內容,不涉及返回調用‘‘‘ info[TimeCreated] = os.path.getctime(dir_obj) #獲取創建時間 info[TimeModified] = os.path.getatime(dir_obj) #獲取訪問時間 info[Size] = os.path.getsize(dir_obj)/1024/1024 #獲取文件大小,單位為M if info[Size] >= 1024: #文件大小換算為G info[Size] = info[Size]/1014 print(%-5s\t%10.2fG\t%30s\t%30s\t%-20s %(file, info[Size], time.ctime(info[TimeCreated]), time.ctime(info[TimeModified]), obj)) elif info[Size] < 1: #文件大小換算問K info[Size] = info[Size] * 1024 print(%-5s\t%10.2fK\t%30s\t%30s\t%-20s %(file, info[Size], time.ctime(info[TimeCreated]), time.ctime(info[TimeModified]), obj)) else: print(%-5s\t%10.2fM\t%30s\t%30s\t%-20s %(dir, info[Size], time.ctime(info[TimeCreated]), time.ctime(info[TimeModified]), obj)) else: #目錄處理 info[TimeCreated] = os.path.getctime(dir_obj) info[TimeModified] = os.path.getatime(dir_obj) info[Size] = GetDirSize(dir_obj)/1024/1024 if info[Size] >= 1024: info[Size] = info[Size]/1014 print(%-5s\t%10.2fG\t%30s\t%30s\t%-20s %(file, info[Size], time.ctime(info[TimeCreated]), time.ctime(info[TimeModified]), obj)) elif info[Size] < 1: info[Size] = info[Size] * 1024 print(%-5s\t%10.2fK\t%30s\t%30s\t%-20s %(file, info[Size], time.ctime(info[TimeCreated]), time.ctime(info[TimeModified]), obj)) else: print(%-5s\t%10.2fM\t%30s\t%30s\t%-20s %(dir, info[Size], time.ctime(info[TimeCreated]), time.ctime(info[TimeModified]), obj)) if __name__ == __main__: getFileInfo(sys.argv[1])

win7執行結果:

E:\chengd_ftp_server\module>python list_dir.py ./
file          7.22K           Mon Aug 14 22:36:24 2017        Sat Aug 19 13:03:43 2017  __pycache__
file          8.47K           Sat Aug 12 10:32:11 2017        Sat Aug 19 00:30:22 2017  threading_socket_server.py
file          1.56K           Sat Aug 12 09:04:21 2017        Mon Aug 14 23:08:05 2017  socket_ftp_main.py
file          3.02K           Sat Aug 19 14:17:31 2017        Sat Aug 19 14:17:31 2017  list_dir.py

centos7執行結果:

[[email protected] tmp]# ls -lah
total 128K
drwxrwxrwt. 15 root root 4.0K Jul 20 18:34 .
dr-xr-xr-x. 17 root root  253 Jul 18 13:03 ..
drwxr-xr-x   2 root root   85 Jul 18 17:32 20170720
drwxr-xr-x   2 root root  103 Jul 19 05:58 20170721
drwxr-xr-x   2 root root  141 Jul 19 13:14 20170722
drwxr-xr-x   2 root root  121 Jul 20 01:33 20170723
drwxr-xr-x   2 root root   39 Jul 20 04:03 20170724
drwxr-xr-x   2 root root   54 Jul 20 17:26 20170801
-rw-r--r--   1 root root  233 Jul 19 23:33 apache.log
-rw-r--r--   1 root root 1.4K Jul 19 23:21 apachen.log
drwxrwxrwt.  2 root root    6 Jun 26 15:37 .font-unix
drwxr-xr-x   2 root root   18 Jul 18 13:08 hsperfdata_root
drwxrwxrwt.  2 root root    6 Jun 26 15:37 .ICE-unix
drwxr-xr-x.  2 root root   30 Jun 27 05:27 shutil_file
drwxrwxrwt.  2 root root    6 Jun 26 15:37 .Test-unix
drwxrwxrwt.  2 root root    6 Jun 26 15:37 .X11-unix
drwxrwxrwt.  2 root root    6 Jun 26 15:37 .XIM-unix
-rw-------   1 root root  97K Jul 18 11:27 yum_save_tx.2017-07-18.11-27.uzI4eR.yumtx
-rw-------   1 root root  15K Jul 18 12:53 yum_save_tx.2017-07-18.12-53.vANJxP.yumtx
[[email protected] tmp]# /root/dir.py .
file 1.72K    Thu Jul 20 17:26:17 2017    Thu Jul 20 17:26:19 2017    20170801 
file 1.65K    Thu Jul 20 04:03:46 2017    Thu Jul 20 07:01:24 2017    20170724 
file 0.23K    Wed Jul 19 23:33:55 2017    Wed Jul 19 23:34:05 2017    apache.log 
file 1.30K    Wed Jul 19 23:21:24 2017    Wed Jul 19 23:19:02 2017    apachen.log 
file 4.88K    Thu Jul 20 01:33:32 2017    Thu Jul 20 18:17:47 2017    20170723 
file 2.13K    Wed Jul 19 13:14:10 2017    Thu Jul 20 18:17:47 2017    20170722 
file 0.97K    Wed Jul 19 05:58:58 2017    Thu Jul 20 18:17:47 2017    20170721 
file 3.17K    Tue Jul 18 17:32:47 2017    Thu Jul 20 18:17:47 2017    20170720 
file 32.00K    Tue Jul 18 13:08:48 2017    Thu Jul 20 18:17:47 2017    hsperfdata_root 
file 14.76K    Tue Jul 18 12:53:05 2017    Tue Jul 18 12:53:05 2017    yum_save_tx.2017-07-18.12-53.vANJxP.yumtx
file 96.68K    Tue Jul 18 11:27:03 2017    Tue Jul 18 11:27:03 2017    yum_save_tx.2017-07-18.11-27.uzI4eR.yumtx
file 0.05K    Tue Jul 18 11:34:08 2017    Thu Jul 20 18:17:47 2017    shutil_file 
file 0.00K    Mon Jun 26 15:44:13 2017    Thu Jul 20 18:17:47 2017    .ICE-unix 
file 0.00K    Mon Jun 26 15:44:13 2017    Thu Jul 20 18:17:47 2017    .font-unix 
file 0.00K    Mon Jun 26 15:44:13 2017    Thu Jul 20 18:17:47 2017    .X11-unix 
file 0.00K    Mon Jun 26 15:37:09 2017    Thu Jul 20 18:17:47 2017    .Test-unix 
file 0.00K    Mon Jun 26 15:37:09 2017    Thu Jul 20 18:17:47 2017    .XIM-unix

文件大小之計算到KB,不是太精確;大家有興趣的可以精確到字節就完美了!

python3獲取指定目錄內容的詳細信息