python读取文件名并改名字的实例

yipeiwu_com6年前Python基础

第一版,能实现,但最后发现文件的顺序改变了:

import os
 
 
def reename():
 nm=1
 pathh="/home/huangyaya/file/image/pic/chips"
 filelist=os.listdir(pathh)
 for files in filelist:
  Olddir=os.path.join(pathh,files)
  filename=os.path.splitext(files)[0]
  filetype=os.path.splitext(files)[1]
  Newdir=os.path.join(pathh,str(nm)+'.'+filetype)
  os.rename(Olddir,Newdir)
  nm+=1
 
reename()

新的

import os
import pdb
 
#dir_ = os.getcwd()
#dir_ += '/cips'
#os.chdir(dir_)
 
 
path_A = "/home/huangyaya/file/image/pic/wine"
path_B = "/home/huangyaya/file/image/pic/wine_output"
file_number = 1
num = 0
A_list = os.listdir(path_A)
B_list = os.listdir(path_B)
A_list_num = 0
B_list_num = 0
 
for A_str in A_list:
 A_str_front = A_str[:-4]
 B_str = A_str_front + '.xml'
 
 os.rename(path_A + '/' + A_str,str(file_number) + '.jpg')
 os.rename(path_B + '/' + A_str_front + '.xml',str(file_number) + '.xml')
 
 file_number += 1

以上这篇python读取文件名并改名字的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python利用matplotlib.pyplot绘图时如何设置坐标轴刻度

Python利用matplotlib.pyplot绘图时如何设置坐标轴刻度

前言 matplotlib.pyplot是一些命令行风格函数的集合,使matplotlib以类似于MATLAB的方式工作。每个pyplot函数对一幅图片(figure)做一些改动:比如创...

30秒学会30个超实用Python代码片段【收藏版】

许多人在数据科学、机器学习、web开发、脚本编写和自动化等领域中都会使用Python,它是一种十分流行的语言。 Python流行的部分原因在于简单易学。 本文将简要介绍30个简短的、...

python实现列表的排序方法分享

python实现列表的排序方法分享

这次代码主要是实现列表的排序,使用sort函数实现,sort函数是对列表中的元素按照特定顺序进行排序,默认reverse,为false,从小到大排序, 如果指定reverse=...

在Python下利用OpenCV来旋转图像的教程

OpenCV是应用最被广泛的的开源视觉库。他允许你使用很少的代码来检测图片或视频中的人脸。 这里有一些互联网上的教程来阐述怎么在OpenCV中使用仿射变换(affine transfor...

python 求某条线上特定x值或y值的点坐标方法

问题可以转换为:求一条垂直于x轴或平行于y轴的直线与该线的交点 import numpy as np import shapely.geometry as SG #某条线 li...