'select and filtered files in directory with enumerating in loop

I have a folder that contains many eof extension files name I want to sort them in ordinary way with python code (as you can see in my example the name of all my files contain a date like:20190729_20190731 and they are just satellite orbital information files, then select and filtered 1th,24th,47th and.... (index ) of files and delete others because I need every 24 days information files( for example:V20190822T225942_20190824T005942) not all days information .for facility I select and chose these information files from first day I need so the first file is found then I should select 24 days after from first then 47 from first or 24 days after second file and so on. I exactly need to keep my desire files as I said and delete other files in my EOF source folder my desire files are like these

S1A_OPER_AUX_POEORB_OPOD_20190819T120911_V20190729T225942_20190731T005942.EOF S1A_OPER_AUX_POEORB_OPOD_20190912T120638_V20190822T225942_20190824T005942.EOF . . . Mr Zach Young wrote this code below and I appreciate him so much I never thought some body would help me. I think I'm very close to the goal the error is
error is print(f'Keeping {eof_file}') I changed syntax but the same error: print(f"Keeping {eof_file}")

enter code here
from importlib.metadata import files
import pprint
items = os.listdir("C:/Users/m/Desktop/EOF")
eof_files = []
for item in items:
# make sure case of item and '.eof' match
if item.lower().endswith('.eof'):
    eof_files.append(item)
eof_files.sort(key=lambda fname : fname.split('_')[5])
print('All EOF files, sorted')
pprint.pprint(eof_files)
print('\nKeeping:')
files_to_delete = []
count = 0
offset = 2
for eof_file in eof_files:
if count == offset: 
print(f"Keeping: [eof_file]")
        # reset count
    count = 0
    continue
 files_to_delete.append(eof_file)
 count += 1
print('\nRemoving:')
for f_delete in files_to_delete:
print(f'Removing: [f_delete]')
staticmethod

 


Solution 1:[1]

Here's a top-to-bottom demonstration.

I recommend that you:

  1. Run that script as-is and make sure your print statements match mine
  2. Swap in your item = os.listdir(...), and see that your files are properly sorted
  3. Play with the offset variable and make sure you can control what should be kept and what should be deleted; notice that an offset of 2 keeps every third file because count starts at 0

You might need to play around and experiment to make sure you're happy before moving to the final step:

  • Finally, swap in your os.remove(f_delete)
#!/usr/bin/env python3
from importlib.metadata import files
import pprint

items = [
    'foo_bar_baz_bak_bam_20190819T120907_V2..._SomeOtherDate.EOF',
    'foo_bar_baz_bak_bam_20190819T120901_V2..._SomeOtherDate.EOF',
    'foo_bar_baz_bak_bam_20190819T120905_V2..._SomeOtherDate.EOF',
    'foo_bar_baz_bak_bam_20190819T120902_V2..._SomeOtherDate.EOF',
    'foo_bar_baz_bak_bam_20190819T120903_V2..._SomeOtherDate.EOF',
    'foo_bar_baz_bak_bam_20190819T120904_V2..._SomeOtherDate.EOF',
    'foo_bar_baz_bak_bam_20190819T120906_V2..._SomeOtherDate.EOF',
    'bogus.txt'
]

eof_files = []
for item in items:
    # make sure case of item and '.eof' match
    if item.lower().endswith('.eof'):
        eof_files.append(item)

eof_files.sort(key=lambda fname : fname.split('_')[5])

print('All EOF files, sorted')
pprint.pprint(eof_files)

print('\nKeeping:')
files_to_delete = []

count = 0
offset = 2
for eof_file in eof_files:
    if count == offset:
        print(f'Keeping  {eof_file}')
        # reset count
        count = 0
        continue

    files_to_delete.append(eof_file)
    count += 1


print('\nRemoving:')
for f_delete in files_to_delete:
    print(f'Removing {f_delete}')

When I run that, I get:

All EOF files, sorted
['foo_bar_baz_bak_bam_20190819T120901_V2..._SomeOtherDate.EOF',
 'foo_bar_baz_bak_bam_20190819T120902_V2..._SomeOtherDate.EOF',
 'foo_bar_baz_bak_bam_20190819T120903_V2..._SomeOtherDate.EOF',
 'foo_bar_baz_bak_bam_20190819T120904_V2..._SomeOtherDate.EOF',
 'foo_bar_baz_bak_bam_20190819T120905_V2..._SomeOtherDate.EOF',
 'foo_bar_baz_bak_bam_20190819T120906_V2..._SomeOtherDate.EOF',
 'foo_bar_baz_bak_bam_20190819T120907_V2..._SomeOtherDate.EOF']

Keeping:
Keeping  foo_bar_baz_bak_bam_20190819T120903_V2..._SomeOtherDate.EOF
Keeping  foo_bar_baz_bak_bam_20190819T120906_V2..._SomeOtherDate.EOF

Removing:
Removing foo_bar_baz_bak_bam_20190819T120901_V2..._SomeOtherDate.EOF
Removing foo_bar_baz_bak_bam_20190819T120902_V2..._SomeOtherDate.EOF
Removing foo_bar_baz_bak_bam_20190819T120904_V2..._SomeOtherDate.EOF
Removing foo_bar_baz_bak_bam_20190819T120905_V2..._SomeOtherDate.EOF
Removing foo_bar_baz_bak_bam_20190819T120907_V2..._SomeOtherDate.EOF

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1