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处理按钮消息的实例详解

python处理按钮消息的实例详解

python处理按钮消息的实例详解            最新学习Python的基础知...

python 函数的缺省参数使用注意事项分析

本文实例讲述了python 函数的缺省参数使用注意事项。分享给大家供大家参考,具体如下: python的函数支持4种形式的参数:分别是必选参数、 缺省参数、 可变长参数、关键字参数;而且...

pandas中的DataFrame按指定顺序输出所有列的方法

问题: 输出新建的DataFrame对象时,DataFrame中各列的显示顺序和DataFrame定义中的顺序不一致。 例如: import pandas as pd grades...

Python多线程处理实例详解【单进程/多进程】

Python多线程处理实例详解【单进程/多进程】

本文实例讲述了Python多线程处理操作。分享给大家供大家参考,具体如下: python — 多线程处理 1、一个进程执行完后,继续下一个进程 root@72132server:~#...

使用TensorFlow实现SVM

使用TensorFlow实现SVM

较基础的SVM,后续会加上多分类以及高斯核,供大家参考。 Talk is cheap, show me the code import tensorflow as tf from s...