只更改文件名的一部分

时间:2017-10-09 13:46:10

标签: python filenames

我在我的文件夹中有这些图像:

  • area11.tif
  • area12.tif
  • area14.tif
  • area21.tif
  • area22.tif
  • area25.tif

如何只更改最后一位数字,以便它们成为有序的"更多增量" ? 相反,area14.tif area13.tif应为try: path = (os.path.expanduser('~\\FOLDER\\')) files = os.listdir(path) idx = 0 for file in files: idx =+ 1 i = 'ex_area' if file.endswith('.tif'): i = i + str(idx) os.rename(os.path.join(path, file), os.path.join(path, str(i) + '.tif')) except OSError as e: if e.errno != errno.EEXIST: raise ,而区域22 /区域25则相同。

我有一个代码,但它有点坏了,因为它删除了一些文件(这很奇怪,我知道......)。

编辑:添加(可能已损坏..)代码

 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainCoordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:id="@+id/mainBottomSheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewOne"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewTwo"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

4 个答案:

答案 0 :(得分:2)

1)将目录中的文件名读入数组(字符串) 2)迭代文件名数组
3)对于每个文件名,对字符串进行切片并插入索引
4)重命名

例如:

import os
import glob

[os.rename(n, "{}{}.tif".format(n[:5], i)) for i, n in  enumerate(glob.glob("area*"))]

答案 1 :(得分:1)

首先,使用glob模块获取图像的列表:

images = glob.glob("/sample/*.tif")

然后你只需用os模块重命名所有这些:

for i in range(len(images)): os.rename(images[i], ‘area’+i+’.tif’) 

答案 2 :(得分:1)

首先将所有文件名重命名为临时名称,然后添加您喜欢的名称

import glob,os
images = glob.glob("*.tif")
for i in range(len(images)):
        os.rename(images[i], 'temp_'+str(i)+'.tif')

tempImages = glob.glob("temp*.tif")

for i in range(len(tempImages)):
        os.rename(tempImages[i], 'area'+str(i+1)+'.tif')

答案 3 :(得分:1)

还找到了这个其他解决方案。但是这个有一个小的区别,最后做一个更好的工作方式(至少对我来说):为每个区域创建一个文件夹。这么简单,我以前没有想过......

BTW,这是代码,评论说。我正在使用这个只是因为我实现了我想要的。感谢所有回答的人,让我学习新事物。

path = (os.path.expanduser('~\\FOLDER\\AREA1\\')) #select folder
files = os.listdir(path)

i = 1 #counter
name = 'area' #variable which the file will take as name

for file in files:
    if file.endswith('.tif'): #search only for .tif. Can change with any supported format
        os.rename(os.path.join(path, file), os.path.join(path, name + str(i)+'.tif')) #name + str(i)+'.tif' will take the name and concatenate to first number in counter. #If you put as name "area1" the str(i) will add another number near the name so, here is the second digit.
        i += 1 #do this for every .tif file in the folder

它有点简单,但因为我将文件放在两个单独的文件夹中。如果将文件保留在同一文件夹中,则无法正常工作。

编辑:现在,我知道,它与我上面的代码相同......

相关问题