簡體   English   中英

無法重命名圖片

[英]Can't rename pictures

下面的代碼允許我重命名文件夾,但我無法重命名.tif文件。 我想重命名Crop1Crop2Crop3中有許多.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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM