delvedor
delvedor

Reputation: 52

Remove elements from a list of file in a directory

I have a little problem and I cannot understand why my program do this thing. Can you help me?

I have a directory with a lot of different files. I need a list of the ".TXT" files (yes, uppercase).

My code:

def listFile():
    files = [f for f in os.listdir('.') if os.path.isfile(f)]
    for f in files:
        if (f[-4:] != ".TXT"):
            del files[files.index(f)]

But I don't know why the list doesn't have only the ".TXT" elements.

My directory's elements:

['OrdinaFile0.3.py', 'OrdinaFile0.4-p2.py', 'OrdinaFile0.4-p3.py', 'OrdinaFile0.5-p2.py', 'OrdinaFile0.6-p2.py', 'TxtToXls-p2.py', 'aula1.TXT', 'testFiles.py', 'testxlwt.py']

The list produced by the code:

['OrdinaFile0.3.py', 'OrdinaFile0.4-p2.py', 'OrdinaFile0.6-p2.py', 'aula1.TXT']

Have you got any idea? Thanks.

Upvotes: 2

Views: 903

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251136

This can be easily done with glob module:

import glob, os
files = [f for f in glob.glob('*.TXT') if os.path.isfile(f)]

Note that not all Operating systems(For eg. Windows) have case sensitive file names, so .txt and .TXT are same for such OS.

The problem with your code is that you're modifying the list while iterating over it, don't do that. Iterate over a shallow copy instead.

From docs:

To change a sequence you are iterating over while inside the loop (for example to duplicate certain items), it is recommended that you first make a copy. Looping over a sequence does not implicitly make a copy. The slice notation makes this especially convenient:

for f in files[:]:
   ...

Upvotes: 4

jramirez
jramirez

Reputation: 8685

Do it all with the comprehension. This way you won't have to iterate through your list and find what files have that .TXT extension. Plus this is more pythonic.

files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.TXT')]

Upvotes: 1

Related Questions