获取文件夹下所有文件的数量和大小
确认电脑有Python的运行环境
在CMD命令行输入:python --version验证
- 如下图,标识已安装ok
编写逻辑
思路介绍
- 指定目录
- 获取当前目录下的文件数组
- 获取文件信息:大小,名称
- 输出到文本中
- 封装部分code为方法,方便调用和识别
import os,time
## 将结果输出到txt文件中
def outputTxt(connect):
w = open('目录说明.txt','a')
w.write('\n ')
w.write('['+time.strftime('%Y.%m.%d %H:%M:%S', time.localtime(time.time()))+'] '+connect)
w.close()
def file_name(file_dir):
for root,dirs,files in os.walk(file_dir):
for file in files:
outputTxt(str(file)+' '+str(os.path.getsize(file)))
file_forder = os.getcwd()
file_name(file_forder)
## print (os.path.abspath('..'))#获得当前工作目录的父目录
## print (os.getcwd())#获得当前目录
## print (os.path.abspath('.'))#获得当前工作目录
## print (os.path.abspath(os.curdir))#获得当前工作目录
生成一个exe可执行文件
并非所有电脑都有python安装环境,比如我们要分享给我们的好友这个好用的方法。
如果还需要对方安装一个程序非常不方便,这里的解决方案就是: pyinstaller
安装链接:http://www.pyinstaller.org/downloads.html
安装方法稍后我会单独介绍,等待我的更新吧~~
》 使用pyinstaller生成exe文件
点击执行效果如下
参考文件:https://zhuanlan.zhihu.com/p/25513571