運營那邊提出需求,有些媒體文件需要統計下
目錄結構大概是這樣的
每個目錄下面都有很多文件,目錄下面沒子目錄
我這里是模擬下創建的目錄和文件,和運營那邊說的目錄結構都是一致的
想最終統計結果如下格式
我的思路如下。
這里肯定用到了操作excel的模塊以及遍歷目錄的模塊
搜索相關遍歷目錄的有os.walk不錯
先練習下它
從結構上來看,for root, dirs, files in os.walk(...),很容易讓人認為os.walk(...)生成了一個迭代器。迭代器的next方法可能會返回下一層次的文件夾內容。事實上,os.walk()是一個生成器函數。
關於生成器轉自下面
https://www.zhihu.com/question/20829330
從結構上來看,for root, dirs, files in os.walk(...),很容易讓人認為os.walk(...)生成了一個迭代器。迭代器的next方法可能會返回下一層次的文件夾內容。
事實上,os.walk()是一個生成器函數。生成器與迭代器,是Python引入的幾大特性之一,而生成器要比迭代器高級一些。至於生成器的工作原理和適用場合,可以先從os.walk()的源碼說起。
>>> def func(n): ... yield n*2 ... >>> func <function func at 0x00000000029F6EB8> >>> g = func(5) >>> g <generator object func at 0x0000000002908630> >>>
func 就是一個生成器函數,調用該函數時返回對象就是生成器 g ,這個生成器對象的行為和迭代器是非常相似的,可以用在 for 循環等場景中。注意 yield 對應的值在函數被調用時不會立刻返回,而是調用next方法時(本質上 for 循環也是調用 next 方法)才返回
>>> g = func(5) >>> next(g) 10 >>> g = func(5) >>> for i in g: ... print(i) ... 10
那為什么要用生成器呢?顯然,用生成器在逼格上要比迭代器高幾個等級,它沒有那么多冗長代碼了,而且性能上一樣的高效,為什么不用呢?來看看用生成器實現斐波那契數列有多簡單。
def fib(n): prev, curr = 0, 1 while n > 0: n -= 1 yield curr prev, curr = curr, curr + prev print([i for i in fib(10)]) #[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
生成器表達式
器表達式與列表推導式長的非常像,但是它倆返回的對象不一樣,前者返回生成器對象,后者返回列表對象。
>>> g = (x*2 for x in range(10)) >>> type(g) <type 'generator'> >>> l = [x*2 for x in range(10)] >>> type(l) <type 'list'>
所以os.walk是生成器函數,可以使用for迭代它
也可以使用next單次獲取結果
# -*- coding:utf-8 -*- import os dir1='D:\Pywork\總目錄' list1=[] inter=os.walk(dir1,topdown=True) print(inter) print(next((inter))) print(next((inter))) print(next((inter)))
結果如下
每次執行結果都是一個三元組,第一個是被遍歷的目錄,第二個是此目錄下的子目錄列表,第三個此目錄下的子文件列表
excel表里,我需要的就是這個列表里的子目錄名,以及子目錄的文件名
# -*- coding:utf-8 -*- import os dir1='D:\Pywork\總目錄' list1=[] for par,dirs1,files1 in os.walk(dir1,topdown=True): print(par,'---------',dirs1,'++++++++',files1) list1.append(dirs1) print('list1---------------------------') print(list1[0]) for zi in list1[0]: print(dir1+'\\'+zi)
運行結果如下
獲取子目錄以及子目錄的文件,打印下
這里先把excel的注釋了
# -*- coding:utf-8 -*- import os import xlwt #寫個配置文件,通過它獲取目錄 def get_dir(): try: with open(r'D:\Pywork\mulu.txt', 'r') as f : print(f.readline()) return f.readline() except Exception as e: print("文件格式有誤,或者文件名不對----"+e) # 創建 xls 文件對象 wb = xlwt.Workbook() # 新增一個表單 sh = wb.add_sheet('文件信息') # 按位置添加數據 dir_col=0 file_col=1 row_init=0 #從配置獲取目錄名 dir=get_dir() for parent1, dir_names1, file_names1 in os.walk(dir): for dir_name1 in dir_names1: for parent2,dir_names2,file_names2 in os.walk(dir+dir_name1): for file_name2 in file_names2: # sh.write(row_init,dir_col,dir_name1) # sh.write(row_init,file_col,file_name2) # row_init=row_init+1 print(dir_name1,file_name2) # wb.save("文件信息結果.xls") # print(dir_name1,file_name2)
上面是通過配置文件方式獲取要被操作的目標目錄
寫入excel運行測試成功
# -*- coding:utf-8 -*- import os import xlwt #寫個配置文件,通過它獲取目錄 def get_dir(): try: with open(r'D:\Pywork\mulu.txt', 'r') as f : print(f.readline()) return f.readline() except Exception as e: print("文件格式有誤,或者文件名不對----"+e) # 創建 xls 文件對象 wb = xlwt.Workbook() # 新增一個表單 sh = wb.add_sheet('文件信息') # 按位置添加數據 dir_col=0 file_col=1 row_init=0 #從配置獲取目錄名 dir=get_dir() for parent1, dir_names1, file_names1 in os.walk(dir): for dir_name1 in dir_names1: for parent2,dir_names2,file_names2 in os.walk(dir+dir_name1): for file_name2 in file_names2: sh.write(row_init,dir_col,dir_name1) sh.write(row_init,file_col,file_name2) row_init=row_init+1 # print(dir_name1,file_name2) wb.save("文件信息結果.xls")
改良下代碼,因為我想把這個py腳本打成exe程序,交給運營。不然每次需要我幫他們弄,太麻煩我了
# -*- coding:utf-8 -*- #exe直接丟入父級目錄下 import os import xlwt #定義個獲取目錄的函數。沒用到 def get_dir(): return os.getcwd() # 創建 xls 文件對象 wb = xlwt.Workbook() # 新增一個表單 sh = wb.add_sheet('子目錄文件信息') # 按位置添加數據,col表示列的意思 dir_col=0 file_col=1 row_init=0 #從配置獲取目錄名,這里並沒用到。因為程序就在目標目錄下。可以重新賦值為. dir=get_dir() dir="." for parent1, dir_names1, file_names1 in os.walk(dir): for dir_name1 in dir_names1: for parent2,dir_names2,file_names2 in os.walk(dir+'\\'+dir_name1): for file_name2 in file_names2: sh.write(row_init,dir_col,dir_name1) sh.write(row_init,file_col,file_name2) row_init=row_init+1 wb.save("子目錄文件統計.xls")
把此py腳本打成exe文件
需要用到PyInstaller以及pywin32
下載的pywin32和PyInstaller為
pywin32-221.win32-py3.5.exe
PyInstaller-3.1.1.tar.gz
Python是沒有自帶訪問windows系統API的庫的,需要下載。庫的名稱叫pywin32,可以從網上直接下載。
以下鏈接地址可以下載:http://sourceforge.net/projects/pywin32/files%2Fpywin32/ (下載適合你的Python版本)
它會自動檢測你的python解釋器路徑並進行安裝(前提是環境變量配置好了)
以下是安裝過程,不用設置環境變量,它會自動檢測你python的環境變量
然后自動安裝到第三方插件里
關於PyInstaller-3.1.1.tar.gz的安裝
我放在下面路徑下了。沒特殊要求
解壓並安裝
我的win10安裝如下 D:\Programs\Python\PyInstaller-3.1.1>python setup.py install
執行
PyInstaller -F 子目錄文件統計.py
-F執行py文件路徑
最好exe程序在下面路徑
注意下面的執行路徑
D:\Programs\Python\PyInstaller-3.1.1\dist\子目錄文件統計.exe
D:\Programs\Python\PyInstaller-3.1.1>PyInstaller -F 子目錄文件統計.py 77 INFO: PyInstaller: 3.1.1 123 INFO: Python: 3.5.2 125 INFO: Platform: Windows-10-10.0.16299-SP0 128 INFO: wrote D:\Programs\Python\PyInstaller-3.1.1\子目錄文件統計.spec 135 INFO: UPX is not available. 137 INFO: Extending PYTHONPATH with paths ['D:\\Programs\\Python\\PyInstaller-3.1.1', 'D:\\Programs\\Python\\PyInstaller-3.1.1'] 144 INFO: checking Analysis 156 INFO: Building because D:\Programs\Python\PyInstaller-3.1.1\子目錄文件統計.py changed 158 INFO: Initializing module dependency graph... 161 INFO: Initializing module graph hooks... 172 INFO: Analyzing base_library.zip ... 2429 INFO: running Analysis out00-Analysis.toc 2596 WARNING: lib not found: api-ms-win-crt-process-l1-1-0.dll dependency of D:\Programs\Python\Python35-32\python35.dll 2692 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of D:\Programs\Python\Python35-32\python35.dll 3031 INFO: Analyzing D:\Programs\Python\PyInstaller-3.1.1\子目錄文件統計.py 3380 INFO: Looking for import hooks ... 3384 INFO: Processing hook hook-encodings.py 3394 INFO: Processing hook hook-pydoc.py 3395 INFO: Processing hook hook-xml.py 3581 INFO: Looking for ctypes DLLs 3587 INFO: Analyzing run-time hooks ... 3599 INFO: Looking for dynamic libraries 3687 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of D:\Programs\Python\Python35-32\DLLs\_ssl.pyd 3827 WARNING: lib not found: api-ms-win-crt-conio-l1-1-0.dll dependency of D:\Programs\Python\Python35-32\DLLs\_hashlib. pyd 3863 INFO: Looking for eggs 3863 INFO: Using Python library D:\Programs\Python\Python35-32\python35.dll 3863 INFO: Found binding redirects: [] 3881 INFO: Warnings written to D:\Programs\Python\PyInstaller-3.1.1\build\子目錄文件統計\warn子目錄文件統計.txt 3892 INFO: checking PYZ 3894 INFO: Building because toc changed 3895 INFO: Building PYZ (ZlibArchive) D:\Programs\Python\PyInstaller-3.1.1\build\子目錄文件統計\out00-PYZ.pyz 4178 INFO: checking PKG 4180 INFO: Building because toc changed 4180 INFO: Building PKG (CArchive) out00-PKG.pkg 5477 INFO: Bootloader D:\Programs\Python\Python35-32\lib\site-packages\pyinstaller-3.1.1-py3.5.egg\PyInstaller\bootloade r\Windows-32bit\run.exe 5478 INFO: checking EXE 5479 INFO: Building because out00-EXE.toc is bad 5490 INFO: Building EXE from out00-EXE.toc 5495 INFO: Appending archive to EXE D:\Programs\Python\PyInstaller-3.1.1\dist\子目錄文件統計.exe D:\Programs\Python\PyInstaller-3.1.1>
注意!!!存儲py文件的時候必須是如下格式
否則打包成exe的時候,會無法成功
把exe程序拿出來
復制放到這里
退出殺毒軟件,雙擊運行
生成xls文件
雙擊打開