Python映射文件夹中的所有文件



你好,我正在编写一个Python脚本,它将映射到列表(或任何其他对象),列表的每个单元格中都有6个项目:

  1. 文件的路径
  2. 文件名(没有完整路径)
  3. 扩展
  4. 创建时间
  5. 上次修改时间
  6. 它是md5散列

我对蟒蛇有点陌生,我尝试了我所知道的一切。。。

有什么帮助吗?

感谢:)

哦,来吧,在谷歌上搜索"python显示文件信息"首先出现的是:

获取有关文件的信息

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

剩下的我交给你。

最新更新