无法重命名多个文件 (Python)
Can't rename multiple files (Python)
我正在尝试重命名一些文件,但我无法弄明白。
这些是我要重命名的文件:
他们是从13开始算的,因为之前还有其他的imges但是他们被删除了(所以只剩下这些)
使用此代码什么都不做。
for (cnt,contours) in cnts:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
roi = gray1[y:y + h, x:x + w]
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.rectangle(thresh_color,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
if (w * h >= 180 * 30):
cv2.imwrite(os.path.expanduser('~\Desktop\FOLDER\ex_area1') + str(idx) + '.tif', roi)
files = glob.glob(os.path.expanduser('~\Desktop\FOLDER\ex_area1') + str(idx) + '.tif')
for file in files:
parts = file.split('_')
new_name = 'ex_{}'.format(parts[0])
os.rename(file, os.path.join(os.path.dirname(file),new_name))
idx
是一个计数器,它为每个图像增加一个数字。它在程序中的这段代码上方声明。
如何重新命名这些 "new" 图片?
ex_area13
和 ex_area14
应该是 ex_area11
, ex_area12
等等..
谢谢
我在想这样的事情:
import os
folder = "."
#tif_files = [i for i in os.listdir(folder) if i.endswith(".tif")]
tif_files = ["ex_area13.tif","ex_area14.tif"]
for ind, file in enumerate(tif_files):
new_name = "ex_area{}.tif".format(str(ind+11).zfill(2)) #11,12... can be changed to 001,002... or other
oldpath = os.path.join(folder,file)
newpath = os.path.join(folder,new_name)
#os.rename(oldpath,newpath)
print("{} --> {}".format(oldpath,newpath))
打印:
./ex_area13.tif --> ./ex_area11.tif
./ex_area14.tif --> ./ex_area11.tif
用这段代码解决了。它还会忽略由于覆盖而导致的错误。对我来说效果很好。
try:
path = (os.path.expanduser('~\FOLDER\')) #path to images folder
files = os.listdir(path)
idx = 0 #counter
for file in files: #search for files..
idx =+ 1
i = 'ex_area1' #name of the file I search for and which will be renamed
if file.endswith('.tif'): #search only for .tif
i = i + str(idx) #add to i the i+counter (namefile+counter)
os.rename(os.path.join(path, file), os.path.join(path, str(i) + '.tif'))
except OSError as e:
if e.errno != errno.EEXIST: #if there are errors of overwrite, ignore them
raise
我正在尝试重命名一些文件,但我无法弄明白。
这些是我要重命名的文件:
他们是从13开始算的,因为之前还有其他的imges但是他们被删除了(所以只剩下这些)
使用此代码什么都不做。
for (cnt,contours) in cnts:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
roi = gray1[y:y + h, x:x + w]
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.rectangle(thresh_color,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('img',img)
cv2.waitKey(0)
if (w * h >= 180 * 30):
cv2.imwrite(os.path.expanduser('~\Desktop\FOLDER\ex_area1') + str(idx) + '.tif', roi)
files = glob.glob(os.path.expanduser('~\Desktop\FOLDER\ex_area1') + str(idx) + '.tif')
for file in files:
parts = file.split('_')
new_name = 'ex_{}'.format(parts[0])
os.rename(file, os.path.join(os.path.dirname(file),new_name))
idx
是一个计数器,它为每个图像增加一个数字。它在程序中的这段代码上方声明。
如何重新命名这些 "new" 图片?
ex_area13
和 ex_area14
应该是 ex_area11
, ex_area12
等等..
谢谢
我在想这样的事情:
import os
folder = "."
#tif_files = [i for i in os.listdir(folder) if i.endswith(".tif")]
tif_files = ["ex_area13.tif","ex_area14.tif"]
for ind, file in enumerate(tif_files):
new_name = "ex_area{}.tif".format(str(ind+11).zfill(2)) #11,12... can be changed to 001,002... or other
oldpath = os.path.join(folder,file)
newpath = os.path.join(folder,new_name)
#os.rename(oldpath,newpath)
print("{} --> {}".format(oldpath,newpath))
打印:
./ex_area13.tif --> ./ex_area11.tif
./ex_area14.tif --> ./ex_area11.tif
用这段代码解决了。它还会忽略由于覆盖而导致的错误。对我来说效果很好。
try:
path = (os.path.expanduser('~\FOLDER\')) #path to images folder
files = os.listdir(path)
idx = 0 #counter
for file in files: #search for files..
idx =+ 1
i = 'ex_area1' #name of the file I search for and which will be renamed
if file.endswith('.tif'): #search only for .tif
i = i + str(idx) #add to i the i+counter (namefile+counter)
os.rename(os.path.join(path, file), os.path.join(path, str(i) + '.tif'))
except OSError as e:
if e.errno != errno.EEXIST: #if there are errors of overwrite, ignore them
raise