Python Forum
Removing items from list if containing a substring
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Removing items from list if containing a substring
#1
I'm trying to identify a file within a directory by removing list items containing an unwanted substring. When the script finishes running, list2 only contains 'test.test1-test-sample.mp4' when it's expected to contain 'test.test-test.mp4' Any help with where my mistake is would be greatly appreciated.

File's in directory: test.test-test.mp4 (intended target file), test.test1-test-sample.mp4, test.test2-test-Sample.mp4

This is not the entire script, just the relevant part. Please ignore the irrelevant imports.
import tmdbsimple as tmdb
import requests
import locale
import os
import subprocess
import shlex
import json
tmdb.API_KEY = ''
api_key = ''


user_input = input("Enter a video file location: ")
dest = input("Enter a destination directory: ")

list1 = []
list2 = []
    
if os.path.isfile(user_input):
    list1.append(os.path.basename(user_input))
    base1 = os.path.splitext(list1[0])
    basename = base1[0]
else:
    list1.append(os.path.basename(user_input))
    basename = list1[0]
    for file in os.scandir(user_input):
        if file.name.endswith(('.mp4', '.mkv', '.avi')):
            list2.append(file.name)
    for j in list2:
        if "-sample" or "-Sample" in j:
            list2.remove(j)
    #target_file = os.path.join(user_input, list2[0])

#print(list1)                
print(list2)
#print(target_file)
Reply


Messages In This Thread
Removing items from list if containing a substring - by pythonnewbie138 - Aug-27-2020, 09:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Substring muzikman 4 243 Dec-01-2020, 03:07 PM
Last Post: deanhystad
  Removing element from list squall 6 285 Nov-22-2020, 09:34 PM
Last Post: jefsummers
  Count number of occurrences of list items in list of tuples t4keheart 1 168 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  concatenating 2 items at a time in a python list K11 3 238 Oct-21-2020, 09:34 AM
Last Post: buran
  Select the other of 2 items in a list Clunk_Head 7 542 Sep-01-2020, 05:27 PM
Last Post: Clunk_Head
  Print the number of items in a list on ubuntu terminal buttercup 2 478 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  removing dictionary element in list using (key, value) MelonMusk 3 504 Jun-13-2020, 02:37 PM
Last Post: buran
  How to put the items of one list in new generated lists Bobbear 1 376 Jun-12-2020, 06:08 AM
Last Post: buran
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 661 May-10-2020, 05:28 AM
Last Post: deanhystad
  mydict.items() is not a list Skaperen 2 466 May-07-2020, 07:03 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020