python3获取指定目录内容的详细信息

不同平台获取指定目录内容的详细信息命令各不相同:

Linux中可以通过ls -al获取获取

python3获取指定目录内容的详细信息

windows中可以通过dir命令获取

python3获取指定目录内容的详细信息

下面是我写的一个通用获取目录内容详细信息的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' %('dir', 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' %('dir', 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执行结果:

[root@localhost 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
[root@localhost tmp]# /root/dir.py .
dir 1.72K Thu Jul 20 17:26:17 2017 Thu Jul 20 17:26:19 2017 20170801
dir 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
dir 4.88K Thu Jul 20 01:33:32 2017 Thu Jul 20 18:17:47 2017 20170723
dir 2.13K Wed Jul 19 13:14:10 2017 Thu Jul 20 18:17:47 2017 20170722
dir 0.97K Wed Jul 19 05:58:58 2017 Thu Jul 20 18:17:47 2017 20170721
dir 3.17K Tue Jul 18 17:32:47 2017 Thu Jul 20 18:17:47 2017 20170720
dir 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
dir 0.00K Mon Jun 26 15:44:13 2017 Thu Jul 20 18:17:47 2017 .ICE-unix
dir 0.00K Mon Jun 26 15:44:13 2017 Thu Jul 20 18:17:47 2017 .font-unix
dir 0.00K Mon Jun 26 15:44:13 2017 Thu Jul 20 18:17:47 2017 .X11-unix
dir 0.00K Mon Jun 26 15:37:09 2017 Thu Jul 20 18:17:47 2017 .Test-unix
dir 0.00K Mon Jun 26 15:37:09 2017 Thu Jul 20 18:17:47 2017 .XIM-unix

文件大小之计算到KB,不是太精确;大家有兴趣的可以精确到字节就完美了!

上一篇:.NET面试问题二


下一篇:linux中文设置