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使用 __init__初始化操作简单示例

本文实例讲述了python使用 __init__初始化操作。分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- # !/usr/bin/python cl...

分析python切片原理和方法

分析python切片原理和方法

使用索引获取列表的元素(随机读取) 列表元素支持用索引访问,正向索引从0开始 colors=["red","blue","green"] colors[0] =="red"...

寻找网站后台地址的python脚本

#!/usr/bin/python # This was written for educational purpose only. Use it at your own risk...

Python用list或dict字段模式读取文件的方法

前言 Python用于处理文本数据绝对是个利器,极为简单的读取、分割、过滤、转换支持,使得开发者不需要考虑繁杂的流文件处理过程(相对于JAVA来说的,嘻嘻)。博主自己工作中,一些复杂的文...

Django之编辑时根据条件跳转回原页面的方法

在要跳转的编辑页面: #首先获取当期的url: curr_url = self.request.GET.urlencode() #创建一个QueryDict对象: params =...