Return to Snippet

Revision: 43944
at April 4, 2011 00:38 by magicrebirth


Updated Code
# simply
import os
os.rename('a.txt','b.kml')

# or

import shutil
shutil.move('a.txt', 'b.kml')


# or if you want to copy..

import shutil
shutil.copy('a.txt', 'b.kml')





# 
# Another example: I am trying to rename a list of files:
# 
# File1.File1
# File2.File2
# 
# etc
# 
# I need them to be renamed to:
# 
# File1
# File2
# etc




import os,shutil 
   
def renamer(target) :    
    os.chdir(target)
    for filename in os.listdir('.') :
        print filename
        newfilename = os.path.splitext(filename)[0] 
        print newfilename
        os.rename(filename, newfilename)
        print "Renamed " + filename + " to " + new_filename

# test the function/module
if __name__ == __main__:
    renamer(sys.argv[1])


# or more simply:

Revision: 43943
at April 4, 2011 00:06 by magicrebirth


Initial Code
# simply
import os
os.rename('a.txt','b.kml')

# or

import shutil
shutil.move('a.txt', 'b.kml')






# 
# Another example: I am trying to rename a list of files:
# 
# File1.File1
# File2.File2
# 
# etc
# 
# I need them to be renamed to:
# 
# File1
# File2
# etc




import os,shutil 
   
def renamer(target) :    
    os.chdir(target)
    for filename in os.listdir('.') :
        print filename
        newfilename = os.path.splitext(filename)[0] 
        print newfilename
        os.rename(filename, newfilename)
        print "Renamed " + filename + " to " + new_filename

# test the function/module
if __name__ == __main__:
    renamer(sys.argv[1])


# or more simply:

Initial URL

                                

Initial Description

                                

Initial Title
Python: rename a file

Initial Tags
file

Initial Language
Python