您好我正在编写一个将映射到列表(或任何其他对象)的Python脚本,列表中的每个单元格中都有6个项目:
我是python的新手,我尝试了所有我知道的事情......
任何帮助?
谢谢:)
答案 0 :(得分:3)
哦来吧,继续google搜索“python show file information”出现的第一件事就是:
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的文档,你会发现参数意味着什么:
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
我会把剩下的部分留给你。