简体   繁体   English

如何在python3中使用语法'*'删除文件?

[英]How to delete files using the syntax '*' with python3?

There are some files that named like percentxxxx.csv,percentyyyy.csv in the dir.I want to delete the files with the name begins with percent. 目录中有一些名为percentxxxx.csv,percentyyyyy.csv的文件。我想删除名称以%开头的文件。

I find the os.remove function maybe can help me,bu I don't konw how to solve the problem. 我发现os.remove函数也许可以帮助我,但我不知道如何解决该问题。

Are there any other functions can delete files using the syntax percent*.csv ? 是否有其他功能可以使用语法percent * .csv删除文件?

The following is my method: 以下是我的方法:

system_dir=os.getcwd()
for fname in os.listdir(system_dir):
    # print(fname)
    if fname.startswith('report'):
        os.remove(os.path.join(system_dir, fname))

I mainly want to know whether there are more easier methed ,for example using * syntax in the method. 我主要想知道是否有更简单的方法,例如在方法中使用*语法。

Use glob : 使用glob

import os
import glob
for csv in glob.glob("percent*.csv"):
  os.remove(csv)

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

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