遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间

import os
import datetime def print_tree(dir_path):
for name in sorted(os.listdir(dir_path)):
full_path = os.path.join(dir_path, name)
file_size = os.path.getsize(full_path)
modify_time = datetime.datetime.fromtimestamp(os.path.getmtime(full_path))
print('%s\t%s\t%s' % (full_path.ljust(26), str(file_size).ljust(6), modify_time))
if os.path.isdir(full_path):
print_tree(full_path) if __name__ == '__main__':
print_tree('e:/test') '''
e:/test\111 4096 2018-05-02 17:42:51.417505
e:/test\111\aaa 0 2018-05-02 17:43:05.443528
e:/test\111\aaa\efg.docx 0 2018-05-02 17:42:57.966647
e:/test\111\abc.xlsx 6610 2018-05-02 17:42:38.974835
e:/test\222 0 2018-05-02 17:21:28.839784
e:/test\333 0 2018-05-02 17:21:32.269475
e:/test\444.txt 0 2018-05-02 17:22:55.221821
e:/test\Hello.txt 0 2018-05-02 17:27:56.921054
e:/test\a123.txt 490 2018-05-06 18:42:55.688596
e:/test\marshal_test.dat 102 2018-05-06 20:32:17.170066
e:/test\sample_pickle.dat 235 2018-05-06 19:37:36.338858
e:/test\sample_struct.dat 18 2018-05-06 20:12:12.599015
e:/test\shelve_test.dat.bak 125 2018-05-06 20:15:30.979789
e:/test\shelve_test.dat.dat 3083 2018-05-06 20:15:30.979789
e:/test\shelve_test.dat.dir 125 2018-05-06 20:15:30.979789
'''

Python小代码_15_遍历指定路径下的所有文件和文件夹,并格式化输出文件路径文件名和文件夹名,文件大小,修改时间的更多相关文章

  1. C#.NET中遍历指定目录下的文件(及所有子目录及子目录里更深层目录里的文件)

    //遍历一个目录下所有的文件列表,代码实例 DirectoryInfo dir = new DirectoryInfo(folderName);var list = GetAll(dir); /// ...

  2. Python小代码_2_格式化输出

    Python小代码_2_格式化输出 name = input("name:") age = input("age:") job = input("jo ...

  3. Python小代码_1_九九乘法表

    Python小代码_1_九九乘法表 max_num = 9 row = 1 while row <= max_num: col = 1 while col <= row: print(st ...

  4. delphi遍历指定目录下指定类型文件的函数

    遍历指定目录下指定类型文件的函数// ================================================================// 遍历某个文件夹下某种文件,/ ...

  5. OpenCV代码提取:遍历指定目录下指定文件的实现

    前言 OpenCV 3.1之前的版本,在contrib目录下有提供遍历文件的函数,用起来比较方便.但是在最新的OpenCV 3.1版本给去除掉了.为了以后使用方便,这里将OpenCV 2.4.9中相关 ...

  6. java-IO流(File对象-深度遍历指定目录下的文件夹和文件)

    需求:遍历这个树状结构 File(String pathname) '\\'为了转义'\' // 通过抽象路径pathname 创建一个新的文件或者目录 File parent = new File( ...

  7. python 实现统计ftp服务器指定目录下文件夹数目、文件数目及所有文件大小

    本次主要为满足应用方核对上传到ftp服务器的文件是否缺漏. 主要要求:指定目录下,文件夹数目/文件数目/所有文件大小,类似Windows如下功能: 模块介绍: from ftplib import F ...

  8. python大法好——变量、常量、input()、数据类型、字符串、格式化输出、运算符、流程控制语句、进制、字符编码

    python基础知识 1.变量 变量:把程序运算的中间结果临时存到内存里,以备后面的代码可以继续调用. 作用:A.存储数据. B.标记数据. 变量的声明规则: A:变量名只能是字母,数字或下划线任意组 ...

  9. perl 遍历指定目录下的所有文件,替换指定文本内容,返回受影响的文件路径

    不会读取 影藏文件 main #!/usr/bin/perl use autodie; use utf8; use Encode qw(decode encode); if(@ARGV ne 3){ ...

随机推荐

  1. 随想录(skyeye中的soc仿真)

    [ 声明:版权所有,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 想学好soc,再怎么看芯片手册和linux kernel都不为过.但是要学习好kernel,那再 ...

  2. Python itertools模块中的product函数

    product 用于求多个可迭代对象的笛卡尔积(Cartesian Product),它跟嵌套的 for 循环等价.即: product(A, B) 和 ((x,y) for x in A for y ...

  3. OpenGL ES 2: debugging, and improvements to VAO, VBO

    OpenGL ES 2: debugging, and improvements to VAO, VBO http://www.altdevblogaday.com/2013/10/12/opengl ...

  4. Ubuntu安装最新版的nodejs

    安装玩Ubuntu的虚拟机之后安装nodejs发现npm的版本才3.5.2,这都多老了?于是Google了一下,发现是由于Ubuntu官方维护的包源太老了,想要安装nodejs的最新版,两种方法,一种 ...

  5. 设置eclipse显示代码错误提示的

    http://jingyan.baidu.com/article/f3e34a128d79aff5ea65356c.html

  6. HihoCoder1127 二分图三·二分图最小点覆盖和最大独立集

    二分图三·二分图最小点覆盖和最大独立集 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在上次安排完相亲之后又过了挺长时间,大家好像都差不多见过面了.不过相亲这个事不是说 ...

  7. HihoCoder 1104 : Suzhou Adventure(树形DP)

    Suzhou Adventure 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi is taking an adventure in Suzhou n ...

  8. Hadoop2.7.1安装与配置

    Hadoop2.7.1集群环境的搭建 s204.s205是我的两台服务器hostname,可以用你对应的ip或者hostname代替 工具/原料   jdk.ssh免登录 方法/步骤   1 首先去A ...

  9. 一起来看CORE源码(一) ConcurrentDictionary

    先贴源码地址 https://github.com/dotnet/corefx/blob/master/src/System.Collections.Concurrent/src/System/Col ...

  10. LeetCode Valid Triangle Number

    原题链接在这里:https://leetcode.com/problems/valid-triangle-number/description/ 题目: Given an array consists ...