2017-01-16 48 views
0

我使用下面的代碼刪除一個CSV文件的內容:刪除多個csv文件的內容與Python

filename = "output.csv" 
# opening the file with w+ mode truncates the file 
f = open(filename, "w+") 
f.close() 

有沒有一種方法來此同時給多個CSV文件,這在同一個文件夾中?基本上我想保留文件名並只刪除他們的內容。

回答

4

可以使用glob包中的標準函數庫擴展目錄中獲取文件的列表:

import glob 

for csv_file in glob.glob("*.csv"): 
    open(csv_file, "w+").close() 
+0

的偉大工程。 Thaks。 – manev