import os import sys # checking whether path and filename are given. if len(sys.argv) != 3: print "Usage : python rename.py <path> <new_name.extension>" sys.exit() # splitting name and extension. name = sys.argv[2].split('.') if len(name) < 2: name.append('') else: name[1] = ".%s" %name[1] # to name starting from 1 to number_of_files. count = 1 # creating a new folder in which the renamed files will be stored. s = "%s/pic_folder" % sys.argv[1] try: os.mkdir(s) except OSError: # if pic_folder is already present, use it. pass try: for x in os.walk(sys.argv[1]): for y in x[2]: # creating the rename pattern. s = "%spic_folder/%s%s%s" %(x[0], name[0], count, name[1]) # getting the original path of the file to be renamed. z = os.path.join(x[0],y) # renaming. os.rename(z, s) # incrementing the count. count = count + 1 except OSError: pass
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question