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

yipeiwu_com5年前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字典中key是否存在的两种方法

今天来说一下如何判断字典中是否存在某个key,一般有两种通用做法,下面为大家来分别讲解一下: 第一种方法:使用自带函数实现。 在python的字典的属性方法里面有一个has_key()方...

python提取照片坐标信息的实例代码

python提取照片坐标信息的代码如下所示: from PIL import Image from PIL.ExifTags import TAGS import os output...

浅谈Python编程中3个常用的数据结构和算法

本篇文章将介绍3种常见的数据结构和同数据有关的算法。此外,在collections模块中也包含了针对各种数据结构的解决方案。 Python内置了许多非常有用的数据结构,比如列表(list...

tensorflow实现简单逻辑回归

逻辑回归是机器学习中很简答的一个栗子,这篇文章就是要介绍如何使用tensorflow实现一个简单的逻辑回归算法。 逻辑回归可以看作只有一层网络的前向神经网络,并且参数连接的权重只是一个值...

Python实现字符串逆序输出功能示例

本文实例讲述了Python实现字符串逆序输出功能。分享给大家供大家参考,具体如下: 1、有时候我们可能想让字符串倒序输出,下面给出几种方法 方法一:通过索引的方法 >>&...