[英]Can't rename pictures
下面的代碼允許我重命名文件夾,但我無法重命名.tif
文件。 我想重命名Crop1
、 Crop2
、 Crop3
中有許多.tif
文件。 代碼似乎運行良好,沒有錯誤,但執行錯誤(即.tif
文件未重命名)。
import os
no = 1
dir = r"C:\Users\Desktop\Crop"
path = str(dir) + str(no)
files = os.listdir(path)
for index, file in enumerate(files):
while no <21:
if file.startswith("Sub"):
os.rename(os.path.join(path , file ), os.path.join(path , 'Oligo'))
no +=1
path = str(dir) + str(no)
您正在更改path
的值,因此在第一次迭代后rename
可能無法在該特定路徑中找到文件。 我猜也許你的意思是
for index, file in enumerate(os.listdir(dir), 1):
while index < 21:
if file.startswith("Sub") and file.lower().endswith((".tif", ".tiff")):
path = dir + str(index)
os.makedirs(path, exists_ok=True) # create directory if it's missing
os.rename(os.path.join(dir, file), os.path.join(path , 'Oligo'))
對於您希望代碼實際執行的操作,顯然存在相當多的猜測,但希望這至少可以幫助您找到方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.