0

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.

I find the os.remove function maybe can help me,bu I don't konw how to solve the problem.

Are there any other functions can delete files using the syntax 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.

  • 2
    Possible duplicate of [How do I delete a file or folder in Python?](http://stackoverflow.com/questions/6996603/how-do-i-delete-a-file-or-folder-in-python) – smac89 Sep 05 '16 at 04:23
  • why reduce the points of the question? – michael_xiang Sep 05 '16 at 05:26
  • I didn't reduce the points (they are called votes btw). I just flagged it as a question that was been asked before and already contains answers. Also flagging does not affect the votes for the answer. Votes go down when people downvote your answer due to not following the [rules and guidelines](http://stackoverflow.com/help/how-to-ask) set for asking questions – smac89 Sep 05 '16 at 18:11
  • thanks,I am not very familiar with stackoverflow – michael_xiang Sep 06 '16 at 08:00

1 Answers1

4

Use glob:

import os
import glob
for csv in glob.glob("percent*.csv"):
  os.remove(csv)
Nehal J Wani
  • 16,071
  • 3
  • 64
  • 89