Trying to take user input and search filenames for ones containing the user input

Hi! So, I am a noob to Python, though i in the past have dabbled in C and Java -

A program i started making as i try to pick up Python, is one that will be able to search a directory of files, and when it finds a filename with certain string in it, it’ll take the file, get rid of the junk text, and copy the new file with the fixed filename to a destination folder.

Example. You have a folder, it’s got a file in it named randomsong_abc[gobbledegook.com].mp3

The program runs, the file gets copied to another predefined folder, and it’s now named randomsong_abc.mp3

At this point, i have this working fully - and it works as is, by having the junk text hard-coded in the program specific things to look for in the filename.
I decided to make another function in this program doing the same thing, BUT to ask for and take user input, and then try to search for THAT.

I am having trouble chasing this down.

With it working so far- that was done using
sys , pathlib (well, pathlib import Path)
and shutil

I can’t find out how to pass user input, to pathlib properly…

Here’s the functional module



def remove_junk_in_title(folder_of_stuff_To_Edit,output_Folder):
    print("\n\n")
    for file in folder_of_stuff_To_Edit.glob("*[[]www.gobbledegook.com[]]*"): 

        print(f'{file}, Files filtered')
        

        renamedfile = file.name.replace(" [www.gobbledegook.com]", "")
        

        RenamedFileWithNewPath = output_Folder / renamedfilename
        print(f' Final Filename will be {renamedfilename},\n Final file will be saved as {RenamedFileWithNewPath}')

      
        shutil.copy2(file, RenamedFileWithNewPath)

    print('List of Corrected Files in proper location:')

    for CorrectlyRenamedFile in output_Folder.glob('*.*'):
        print(f'{CorrectlyRenamedFile}')

So that one works, here’s the function i’m working now
I’m just starting out trying to get it to print matching filenames, before i copy the rest of the
code over for it to make the change and put the new named files in a location.


def remove_specific_junk_in_filename(to_edit_folder, edited_folder):

for unedited_files in folder_of_stuff_To_Edit.glob('*.*'):
    print({unedited_files})    #Listing what we have, to select from

junk_text = input(" Enter string you want to remove from  all filenames containing it")

for file in to_edit_folder.glob(junk_text):  
    print(f'{file}, searched, File filtered. \n')

This doesn’t work-

I’ve been searching for examples and looking at the documentation on passing something to .glob, since i think the issue is at the for file in stuff_To_Edit.glob(junk_text) section. I want the user to be able to search directly- and when i test it right now and try to have it print it right back out, nothing prints out when i type in [www.gobbledegook.com].

I asked some resources ,and got the answer back that i needed to recompile the thing i’m searching for in filenames as regex. I was told to use re.compile, in a way like

 pattern = re.compile(junk_text)

for file in stuff_To_Edit.glob(pattern):  
            print(f'{file}, searched, File filtered. \n'
  • but that wasn’t quite it- I got a attribute error. Looked into it as well, but nothing resembled what i’m trying to do
AttributeError: 're.Pattern' object has no attribute 'replace'

I am still running into dead ends trying to look up how to send a variable to .glob
I DID briefly try , a few variations of

 file in stuff_To_Edit.glob(input())

to get it to directly take input, but this didn’t work. I’m back to attempting to find a good example of how to use pathlib to search filenames, but involving the user inputting the search term instead of pre-defined hardcoded text. I haven’t seen anything involving pathlib about re.compile,either, but am still looking…

What am i missing here?