Last active
August 29, 2015 14:21
-
-
Save pranavgarg/0f0dbc816694b33b2a7d to your computer and use it in GitHub Desktop.
Renaming of files in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Before filename: recoded_videos%2Falgs4partI-64.mp4 | |
new_file: 64.mp4 | |
""" | |
import os | |
directory = '<DIR NAME>' | |
pattern_to_be_excluded = '.mp4' | |
pattern_to_be_excluded_2 = '-' | |
k = os.walk(directory) | |
files = k.next()[2] #getting the files from generator | |
for file in files: | |
if pattern_to_be_excluded not in file: | |
print "not touching file: %s" %(file) | |
continue | |
if pattern_to_be_excluded_2 not in file: | |
print "cannot split the filename: %s" %(file) | |
continue | |
new_file = file.split('-')[1] | |
print "renaming the file %s with new_file:%s " %(file, new_file) | |
os.rename(directory + '/' + file, directory + '/' + new_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment