我有用於測試的目錄中包含的大量文件集。我需要保留我的應用程序的目錄結構,但是爲了加快測試速度,我們希望精簡這些文件。我想限制一個目錄可以擁有的文件數目。3.我怎樣才能在linux中做到這一點?遞歸刪除除了每個目錄中的某個數字以外的所有文件
爲了澄清我想完成,解決在Python什麼:
import sys, os
for root, dirs, files in os.walk(sys.argv[1]):
for index, file in enumerate(files):
if index > int(sys.argv[2]) - 1: os.remove(os.path.join(root, file))
用法:
python thinout.py /path/to/thin\ out/ <maximum_number_of_files_per_directory>
例子:
python thinout.py testing\ data 3
我發現a smiliar question about doing this for one directory, but not recursively。
第一個分號後不需要「做」嗎? – EJK
是的,修正。謝謝。 – Matt
正如@Perleone暗示的,這個版本不適用於需要轉義的目錄名稱(例如,帶有嵌入空格的名稱)。 – mklement0