Home > Backend Development > Python Tutorial > python实现批量改文件名称的方法

python实现批量改文件名称的方法

WBOY
Release: 2016-06-10 15:11:35
Original
1075 people have browsed it

本文实例讲述了python实现批量改文件名称的方法。分享给大家供大家参考。具体分析如下:

发现python中提供了大量的模块函数,有时候一些系统操作在python中非常简单

下面的文件关键是要放到要操作的目录下, 下面是把当前目录下的图片批量命名,从00开始,其中小于10 的我们在名称前面补零,或者可以利用os设置路径

#-*- coding: UTF-8 -*-
import os
filenames = os.listdir(os.getcwd())
for name in filenames:
  print(name)
for num in range(0,len(filenames)):
  if(num<10):
    print()
    print(filenames[num])
    os.rename(filenames[num],'0'+str(num)+'.png')
  else:
    os.rename(filenames[num],str(num)+'.png')

Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template