欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python 2.7.15 / Python 3.6.5 - os.rename - 深度学习 数据集 批量规范命名

程序员文章站 2022-07-13 15:39:59
...

Python 2.7.15 / Python 3.6.5 - os.rename - 深度学习 数据集 批量规范命名

1. Python 2.7.15
Python os.rename() 方法
os.rename() 方法用于命名文件或目录,从 src 到 dst,如果 dst 是一个存在的目录,将抛出 OSError。
src -- 要修改的目录名
dst -- 修改后的目录名
os.rename(src, dst)
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file; there may be no way to implement an atomic rename when dst names an existing file.
Availability: Unix, Windows.
2. Python 3.6.5
Python3 os.rename() 方法
os.rename() 方法用于命名文件或目录,从 src 到 dst,如果 dst 是一个存在的目录,将抛出 OSError。
src -- 要修改的目录名
dst -- 修改后的目录名
os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail on some Unix flavors if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement). On Windows, if dst already exists, OSError will be raised even if it is a file.
This function can support specifying src_dir_fd and/or dst_dir_fd to supply paths relative to directory descriptors.
If you want cross-platform overwriting of the destination, use replace().
New in version 3.3: The src_dir_fd and dst_dir_fd arguments.
Changed in version 3.6: Accepts a path-like object for src and dst.
3. 深度学习 数据集 批量规范命名
3.1 原始图像命名

Python 2.7.15 / Python 3.6.5 - os.rename - 深度学习 数据集 批量规范命名
3.2 rename_file_multiple_images.py
#
# Created by foreverstrong
#

import os
import sys

print("\nsys.version:")
print(sys.version)

image_path = "/home/strong/demo_workspace/rename_file/image_data"


image_dir = image_path + '/'
filepath = os.listdir(image_dir)

# filepath.sort()

image_path_new = image_path + "_rename"
image_dir_new = image_path_new + '/'

if not os.path.exists(image_dir_new):
    os.makedirs(image_dir_new)


num = 0

for imagename in filepath:

    oldname = image_dir + imagename

    padnum = str(num + 1).zfill(6)
    newname = image_dir_new + "image_" + padnum + ".jpg"

    os.rename(oldname, newname)
    print(oldname, ">>>>>>======>>>>>>", newname)

    num += 1

3.3 批量规范命名
/home/strong/demo_workspace/rename_file/image_data_rename
Python 2.7.15 / Python 3.6.5 - os.rename - 深度学习 数据集 批量规范命名
/home/strong/demo_workspace/rename_file/image_data
Python 2.7.15 / Python 3.6.5 - os.rename - 深度学习 数据集 批量规范命名

Python 2.7.15 / Python 3.6.5 - os.rename - 深度学习 数据集 批量规范命名

References
https://docs.python.org/2.7/library/os.html
https://docs.python.org/3.6/library/os.html