Stuck at os.rename can't quite figure out how to tie it in with the rest of my script
Question: I am new to programming and python and this is my first program I decided to
try and tackle.

This loops through my files in the directory that I run it from and takes out
the string of text I don't want and leaves me with the file name I would like
(in theory). It all works except the problem I am stuck with is that I can not
figure out how to get `os.rename` (I think that is what I need to use) to work
from inside my loop.

I have read up on it but I guess I just don't get how to tie it in. I'm using
python 2.7. This is what I have got so far:

    
    
    import os
    
    file_count = 0
    
    for files in os.listdir('.'): #Open for loop in the current dir.
        if files.find('_The_Hype_Machine_') and files.endswith('.128.mp3') :
            mod_list = list(files) #Turns filenames into a list so they can be edited
            del(mod_list[-38:-4])  #specifies the piece of string I need taken out of each filename.
            files =''.join(mod_list) #Turns the list back to a string
            file_count += 1 
            updated_file = files.replace('_', ' ') 
            os.rename(files, **"not sure what goes here"**)
    
            print updated_file
            print 'Your modifed MP3 File Count: ', file_count;  
    

I could use some direction and Help with understanding if anyone is up for it.
Thanks in advance.


Answer: I've made a few tweaks to your program, and annotated it with instructional
comments:

    
    
    import os
    
    # Each time through the following loop, "afile" will take on the next file name
    # also we can use "enumerate" to give us a file_count as we go (instead 
    # of tracking the count separately)
    # In general, we shouldn't change a list we're iterating over, so we'll save it off
    file_list = os.listdir('.')  # (as sk4x0r mentioned)
    for file_count, afile in enumerate(file_list): #Open for loop in the current dir.  
    
        # using the 'some_string' in some_text is more typical python useage
        if '_The_Hype_Machine_' in afile and afile.endswith('.128.mp3'):
    
            # Now, strings are immutable, so we can't exactly remove the 
            # inside of a string
            # What we can do is create a brand new string composed 
            # of everything we want to keep
            new_name = afile[:-38] + afile[-4:]
    
            updated_file = new_name.replace('_', ' ') 
            os.rename(afile, updated_file)
    
            print updated_file
            print 'Your modifed MP3 File Count: ', file_count
    

