-1

I have some code that writes out files with names like this:

body00123.txt
body00124.txt
body00125.txt

body-1-2126.txt
body-1-2127.txt
body-1-2128.txt

body-3-3129.txt
body-3-3130.txt
body-3-3131.txt

Such that the first two numbers in the file can be 'negative', but the last 3 numbers are not.

I have a list such as this:

123
127
129

And I want to remove all the files that don't end with one of these numbers. An example of the desired leftover files would be like this:

body00123.txt

body-1-2127.txt

body-3-3129.txt

My code is running in python, so I have tried:

if i not in myList:
     os.system('rm body*' + str(i) + '.txt')

And this resulted in every file being deleted.

The solution should be such that any .txt file that ends with a number contained by myList should be kept. All other .txt files should be deleted. This is why I'm trying to use a wildcard in my attempt.

1 Answers1

0

Because all the files end in .txt, you can cut that part out and use the str.endswith() function. str.endswith() accepts a tuple of strings, and sees if your string ends in any of them. As a result, you can do something like this:

all_file_list = [...]
keep_list = [...]

files_to_remove = []

file_to_remove_tup = tuple(files_to_remove)

for name in all_file_list:
    if name[:-4].endswith(file_to_remove_tup)
        files_to_remove.append(name)
        # or os.remove(name)
Dash
  • 1,191
  • 7
  • 19
  • What is the significance of [-7:-4]? – FrustratedSnake Nov 25 '22 at 23:08
  • You can also do `files_to_remove = [file for file in all_file_list if file[-7:-4] not in keep_list]`. Here, `keep_list` would have to be strings, not ints. – MattDMo Nov 25 '22 at 23:09
  • @FrustratedSnake negative indexing into the string by `[-7:-4]` gets the three characters before the `.txt`, which are the three numbers that identify which files you want to keep – Dash Nov 25 '22 at 23:09
  • https://stackoverflow.com/questions/509211/understanding-slicing – MattDMo Nov 25 '22 at 23:10
  • What if the last 3 numbers could actually also be 4 numbers? Such that there could be files like body001000.txt , and body001005.txt? – FrustratedSnake Nov 25 '22 at 23:12
  • @FrustratedSnake So do you always need to check if the 3- and 4-digit versions exist in the list? – Dash Nov 25 '22 at 23:16
  • The only condition is that any .txt file that ends with a number that is contained by myList must be kept. All other .txt files should be deleted. – FrustratedSnake Nov 25 '22 at 23:19
  • @FrustratedSnake I've updated the code to support searching from the end of any length string – Dash Nov 25 '22 at 23:35