【发布时间】:2019-11-25 14:41:27
【问题描述】:
您好,我有不同的 CSV 文件,但其中一些在中间有相似的名称。 文件名: 1. ab.New_Groups.3123.csv 2. bca.New_Groups.2134.csv 3. asdad.DDog.213.csv 4. asda.Cat.12312.csv
这是我目前所拥有的:
import glob
import zipfile
import os
#will unzip the file into a folder
with zipfile.ZipFile("October.zip", 'r') as zip_ref:
zip_ref.extractall(("/path/Reports"))
#deleting files which are not useful
extension = ".csv"
#going to the directory where the files at
os.chdir("path/Reports")
all_filenames = [f for f in glob.glob('[New_Groups]*{}'.format(extension))]
#for d in all_filenames:
os.remove(d)
print all_filenames
它不会删除任何内容,我的目标是删除名称中包含“New_Groups”的所有文件。
提前谢谢你!
【问题讨论】:
-
os.remove(d)应该删除文件d,但在all_filenames中,您拥有在文件存在时检索到的文件的原始列表。如果该文件确实仍然存在,请检查 windows 文件夹浏览器。 -
用python删除文件?为什么不简单地
ls | grep New_Groups | xargs rm? -
在我的脚本中,我将它作为#comment 它不是它的实际部分。 @Aryerez
-
@meissner_ 它是一个大脚本的一部分,我正在尝试将所有内容保存在 python 中,可能会调用一个 bash 脚本,这是一个好主意,谢谢!!1
标签: python csv data-analysis