Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os # Imports the OS function
- import time # Imports the time function
- import glob # Imports the glob function
- os.system("cls") # Cleans the screen, change this to your relevant console command for your OS.
- print "\n What is the current extension of the file?" # Asks the user for the current file extension
- ext_0 = raw_input(" > ") # Explains its self, asks the user to set the variable for ext_0
- os.system("cls")
- print "\n What is the new extension of the file?" # I wanted to change some of my pictures extension type from png or bmp to jpg so I just used this to rename it rather than convert it.
- ext_1 = raw_input(" > ") # Again, explains its self.
- os.system("cls")
- print "\n What would you like the file to be named?" # Sets a prefix so LKP_ would be what you enter in here!
- filename = raw_input(" > ")
- os.system("cls")
- print "\n What is the directory of the files?" # Go from the relevant directory, I currently use C:/ as my main directory because it just makes it easier for my script to run, however you can run it from same directory or even a parent directory.
- dir = raw_input(" > ")
- os.system("cls")
- os.chdir(dir) # Uses the OS function to change the directory to the directory of the file
- dir_list = glob.glob("*"+ext_0) # Lists all the files that have the same extension type as the user input
- num_0 = 1 # used in the while statement to add to the file name using my earlier example = LKP_* astrix would be the number so the first file it would make would be LKP_1.jpg
- num_1 = len(dir_list) # Counts the amount of files so it knows how many times to run the while code.
- num_2 = 0 # This is used in the "less" than for the while.
- while num_2 < num_1: # I could have used a For loop or something else but I just wrote this as a quick script
- part_1 = dir+dir_list[num_2] # Part one of the os.rename, it makes it easier to see what's going on this way.
- part_2 = dir+filename+str(num_0)+ext_1 # Part two of the os.rename, it makes it easier to see what's going on this way.
- os.rename(part_1,part_2) # Renames all the relevant files.
- print "\n Renamed: "+dir_list[num_2] # Just prints out the number of files renamed, looks rather pretty.
- num_0 = num_0 + 1 # Just adds the count to the loop so it doesn't run infinitely.
- num_2 = num_2 + 1 # See above
- time.sleep(1) # lets you see it's done for a brief second.
- os.system("cls")
- print "\n Done!"
- time.sleep(2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement