I am learning Python and I have been tasked with:
- adding "file_" to the beginning of each name in a directory
- changing the extension (directory contains 4 different types currently: .py, .TEXT, .rtf, .text)
I have many files, all with different names, each 7 characters long. I was able to change the extensions but it feels very clunky. I am positive there is a cleaner way to write the following (but its functioning, so no complaints on that note):
    import os, sys
    path = 'C:/Users/dana/Desktop/text_files_2/'
        for filename in os.listdir(path):
            if filename.endswith('.rtf'):
                newname = filename.replace('.rtf', '.txt')
                os.rename(filename, newname)
        elif filename.endswith('.py'):
                newname = filename.replace('.py', '.txt')
                os.rename(filename, newname)
        elif filename.endswith('.TEXT'):
                newname = filename.replace('.TEXT', '.txt')
                os.rename(filename, newname)
        elif filename.endswith('.text'):
               newname = filename.replace('.text', '.txt')
               os.rename(filename, newname)
I do still have a bit of a problem:
- the script currently must be inside my directory for it to run. 
- I can not figure out how to add "file_" to the start of each of the filenames [you would think that would be the easy part]. I have tried declaring newname as - newname = 'file_' + str(filename)- it then states filename is undefined. 
Any assistance on my two existing issues would be greatly appreciated.
 
     
     
     
    