Commit c1a1afbf4b0cb05254e5a09df958b4821a576282
1 parent
c0bf33cd
Exists in
master
Adiciona opção de sobreescrever arquivo e/ou usar estampa de tempo na função renomear
Showing
1 changed file
with
46 additions
and
14 deletions
Show diff stats
pyutil.py
... | ... | @@ -87,22 +87,54 @@ def file_exists(filePath): |
87 | 87 | else: |
88 | 88 | return False |
89 | 89 | |
90 | -def file_rename(filename, fromfile): | |
90 | +# @def Função para renomear arquivo de video gerado pelo blender | |
91 | +# @see entrada: /temp/arquivo_video_0001-0250.mp4 | |
92 | +# @see saida: /temp/arquivo_video.mp4 | |
93 | +# @param string fileFullPath: Caminho do arquivo a ser renomeado | |
94 | +# @param bool useTimeStamp: Renomeia o arquivo de acordo com a data e hora do sistema | |
95 | +# @param bool overwrite: Sobreescreve o arquivo | |
96 | +# @return string Retorna o nome do arquivo renomeado | |
97 | +def file_rename(fileFullPath, useTimeStamp = False, overwrite = True): | |
91 | 98 | from shutil import move |
92 | - newFilename = "" | |
93 | - isValidChar = True | |
99 | + from os.path import abspath, basename, dirname, join, splitext | |
100 | + if (file_exists(fileFullPath) == False): | |
101 | + return "" | |
102 | + filePath = dirname(abspath(fileFullPath)) | |
103 | + filename = basename(splitext(fileFullPath)[0]) | |
104 | + extension = splitext(fileFullPath)[1] | |
105 | + filenameInv = "" | |
106 | + isValidChar = False | |
107 | + | |
108 | + # Percorre o "filename" do final para o inicio copiando caracteres após o primeiro '_' encontrado | |
94 | 109 | for char in reversed(filename): |
95 | 110 | if (isValidChar == True): |
96 | - newFilename += char | |
111 | + filenameInv += char | |
97 | 112 | if (char == '_'): |
98 | 113 | isValidChar = True |
99 | - elif (char == '.'): | |
100 | - isValidChar = False | |
101 | - if (len(filename) != len(newFilename)): | |
102 | - try: | |
103 | - move(filename, newFilename[::-1]) | |
104 | - return 1 | |
105 | - except Exception as e: | |
106 | - printStackTrace(fromfile) | |
107 | - return 0 | |
108 | - return 0 | |
114 | + try: | |
115 | + | |
116 | + # Inverte sequencia de caracteres que foi copiada | |
117 | + filenameInv = filenameInv[::-1] | |
118 | + | |
119 | + if (useTimeStamp == True): | |
120 | + # Recupera novo fullPath + nome do arquivo + data/hora + extensão | |
121 | + newFilename = join(filePath, "%s_%s%s" % (filenameInv, getTimeStamp(), extension)) | |
122 | + else: | |
123 | + # Recupera novo fullPath + nome do arquivo + extensão | |
124 | + newFilename = join(filePath, "%s%s" % (filenameInv, extension)) | |
125 | + | |
126 | + # Enumera o nome do arquivo caso o parametro "overwrite" seja "False" e o arquivo já exista | |
127 | + if (overwrite == False): | |
128 | + count = 0 | |
129 | + while (file_exists(newFilename) == True): | |
130 | + count += 1 | |
131 | + # newFilename = join(filePath, "%s_%i%s" % (filenameInv, count, extension)) | |
132 | + newFilename = join(filePath, "%s_%0.4i%s" % (filenameInv, count, extension)) | |
133 | + | |
134 | + log("Rename: '%s' to: '%s'" %(fileFullPath, newFilename)) | |
135 | + move(fileFullPath, newFilename) | |
136 | + return newFilename | |
137 | + | |
138 | + except Exception as e: | |
139 | + printStackTrace(__file__) | |
140 | + return "" | ... | ... |