Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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
You can’t perform that action at this time.