Skip to content

Instantly share code, notes, and snippets.

@absentm
Created July 13, 2016 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save absentm/4923480560554ab87af611049a1cef14 to your computer and use it in GitHub Desktop.
Save absentm/4923480560554ab87af611049a1cef14 to your computer and use it in GitHub Desktop.
# coding: utf-8
import os
import shutil
import sys
# get the filename without extra extend name
def getImageFilePre(filename):
if filename.endswith(".png"):
temp = filename.split(".")
filePre = temp[0]
return filePre
# string 转 int
def str2Int(stringValue):
return int(stringValue)
# int 转 string
def int2Str(intValue):
return str(intValue)
# file rename
def fileRename(dirPath):
# three params:
# 1.parent catalog
# 2.all the catalogs' name
# 3.all the file names
for parent, dirnames, filenames in os.walk(dirPath):
for dirname in dirnames:
count = 1
newTmpPath = os.path.join(dirPath, dirname)
os.chdir(newTmpPath)
fileContents = os.listdir(newTmpPath)
for curFile in fileContents:
if curFile.endswith(".png"):
newName = dirname + "."+ int2Str(count) + ".png"
count = count + 1
shutil.move(curFile, newName)
print curFile + " -> " + newName + " ------> OK!"
def main():
rootPath = "~\\data_origin\\train_with_class"
fileRename(rootPath)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment