简体   繁体   English

使用python删除多个csv文件的内容

[英]Deleting the content of multiple csv file with python

I am using the following code to delete the content of a single csv file: 我正在使用以下代码删除单个csv文件的内容:

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

Is there a way to this at the same time to multiple csv files which are in the same folder? 有没有一种方法可以同时处理同一文件夹中的多个csv文件? Basically I want to keep the filenames and only to delete their content. 基本上,我想保留文件名,而只删除它们的内容。

You can use the glob package in the standard library to get a list of files by extension in a directory: 您可以使用标准库中的glob包来获取目录中扩展名的文件列表:

import glob

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM