0

I am getting above error when I am trying to remove .zip files from my directory list

>>> from os import listdir
>>> from os.path import isfile,join
>>> dr = listdir(r"C:\Users\lenovo\Desktop\ronit")
>>> dr

output:

['7101', '7101.zip', '7102', '7102.zip', '7103', '7103.zip']

Now for removing .zip files I wrote following code:

>>> dr.remove("*.zip")

output:

Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
dr.remove("*.zip")
ValueError: list.remove(x): x not in list

where am I going wrong?

3
  • 2
    Do you want to remove it from your directory? Or the variable? Commented Jun 13, 2017 at 18:59
  • Isn't it obvious from the error? "*.zip" is not in your list... Commented Jun 13, 2017 at 19:09
  • Can anyone tell my why this actually does nothing? thanks names = [x for x in names if x is not None or '00000'] Id frankly like to clean this up the whole thing is names= list(df_ora['CODE'].unique()) names.append("00000") names = [x for x in names if x != None] names = [x for x in names if x != '00000'] Commented Apr 12, 2018 at 14:43

2 Answers 2

2

You cannot use wildcards when removing from a list, you have to iterate through it if you want to do away with partial matches, e.g.:

filtered_list = [file_name for file_name in dr if file_name[-4:] != ".zip"]
# ['7101', '7102', '7103']
Sign up to request clarification or add additional context in comments.

5 Comments

Just out of curiosity, any reason for not using endswith?
I think it would be more robust if you used .endswith() instead of relying on slice notation.
@Wondercricket - performance... str.endswith() does exactly the same, just after going through a couple of hoops. You get 10-20% speed increase in dependence of Python version and it's not as if it's cryptic or unreadable to be considered 'non-Pythonic'.
@zwer str.endswith seems easier to extend (with a tuple) for additional file extensions and some may not be 4 characters long (including the period)
Let me be clear, I'm not denying the benefits of str.endswith() - it does the dirty work of figuring out the length of a passed string and creates a slice based on that which in certain situations can be faster than doing your own slicing (as it happens on the C side in CPython)... I'm denying it's 'usefulness' (or rather advocate for better performant versions) in strict cases like this one where we upfront know what we're looking for.
0
import os
from os import listdir
from os.path import join

dir = 'C:\\Users\\lenovo\\Desktop\\ronit'
dr=os.listdir(dir)

for f in dr:
    if item.endswith(".zip"):
        os.remove(join(dir, f))

1 Comment

Seeing how the other answer was accepted, it can be assumed that the OP was not asking for removing the file from the directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.