哦,来吧,继续谷歌搜索“python显示文件信息”出现的第一件事是:
Getting Information About a File
This function takes the name of a file, and returns a 10-member tuple with the following contents:
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)
然后你去python的文档,你会发现参数是什么意思:
os.stat
st_mode - protection bits,
st_ino - inode number,
st_dev - device,
st_nlink - number of hard links,
st_uid - user id of owner,
st_gid - group id of owner,
st_size - size of file, in bytes,
st_atime - time of most recent access,
st_mtime - time of most recent content modification,
st_ctime - platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows)
那你去看看如何列出一个dir函数,这也在名为listdir的文档中。不要告诉我这很难,我花了 1 分钟。
这是使用 DFS(深度优先搜索)遍历槽文件夹的方法:
import os
def list_dir(dir_name, traversed = [], results = []):
dirs = os.listdir(dir_name)
if dirs:
for f in dirs:
new_dir = dir_name + f + '/'
if os.path.isdir(new_dir) and new_dir not in traversed:
traversed.append(new_dir)
list_dir(new_dir, traversed, results)
else:
results.append([new_dir[:-1], os.stat(new_dir[:-1])])
return results
dir_name = '../c_the_hard_way/Valgrind/' # sample dir
for file_name, stat in list_dir(dir_name):
print file_name, stat.st_size # sample with file size
剩下的交给你。