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访问纯真IP数据库的代码

核心代码: #!/usr/bin/env python # -*- coding: utf-8 -*- from bisect import bisect _LIST1,...

用Python脚本生成Android SALT扰码的方法

复制代码 代码如下:#!/usr/bin/python   # Filename: gen_salt.py   import random&nbs...

Python探索之修改Python搜索路径

当Python执行import语句时,它会在一些路径中搜索Python模块和扩展模块。可以通过sys.path查看这些路径,比如: >>> import sys...

python中的常量和变量代码详解

局部和全局变量: # name='lhf' # def change_name(): # # global name # name='帅了一比' # print('cha...

Python线程条件变量Condition原理解析

这篇文章主要介绍了Python线程条件变量Condition原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Condition...