how to delete hyphen in filename and add something to the name ? python -


I have such a folder with a lot of files:

  2012-05- 09 .txt 2012-05-10.txt 2012-05-11.txt etc.   

I want to remove the hyphen and now I want to add "_PHOTOS_LOG" which it looks at the end:

  20120509_PHOTOS_LOG.txt 20120510_PHOTOS_LOG.txt 20120511_PHOTOS_LOG.txt etc.   

How to do that?

The code is now:

  // code has been updated, now its managed import import import import os.path SYS src = 'D: \\ test / Hyphen1 'src = r'D: \ test \ test' oslistdir (src): new_filename = fn.replace ('-', '') for FN Replace ('.txt', '_PHOTOS_LOG.txt') Fn = os.path.join (src, fn) new_filename = os.path.join (src, new_filename) Try: excluding os.rename (fn, new_filename) (WindowsError, OSError): Print 'Error' name changed to "% s" in "% S" '% (fn, new_filename) print sys.exc_info () [1]    < P> 

You change something like this:

  import file for oslistdir (".") File name: If filename.endswith (".txt"): newFilename = filename. ("-", "") os.rename (filename, new filename [: 7] + "_ PHOTO_LOG.txt")   

named os.listdir (".") Entries in the current folder filename.endswith (".txt") confirms that if the file name is one of your text files, if it is, then it is deleted - and it adds the end to the HOTO_LOG

Comments