#手动选择路径,批量改名

import os,re,time,tkFileDialog
global i
#文件名后面增加后缀:txt,png,bng,jpeg,jpg,gif,zip类型的文件
def change_name(path,text):
global i
if not os.path.isdir(path) and not os.path.isfile(path):
return False
elif os.path.isfile(path):
file_text=os.path.split(path)
lists=file_text[-1].split('.')
os.rename(path,file_text[0]+'\\'+lists[0]+text+'.'+lists[-1])#添加后缀
i+=1
elif os.path.isdir(path):
for x in os.listdir(path):
change_name(os.path.join(path,x), text)
#修改后缀名,将图片统一修改为jpg
def change_ext(path):
global i
if not os.path.isdir(path) and not os.path.isfile(path):
return False
elif os.path.isfile(path):
file_text=os.path.split(path)
lists=file_text[-1].split('.')
mange=['jpeg','png','gif','bng','psd']
if lists[1] in mange:
os.rename(path,file_text[0]+'\\'+lists[0]+'.'+'jpg')
i+=1
elif os.path.isdir(path):
for x in os.listdir(path):
change_ext(os.path.join(path,x)) print'0代表更换文件名,其它代表改扩展名'
name=int(raw_input('请选择更换文件名还是改变扩展名:'))
lj=tkFileDialog.askdirectory()
i=0
if name==0:
gb_name=str(raw_input('输入添加的后缀:'))
start_time=time.time()
change_name(lj,gb_name)
end_time=time.time()
print '开始时间:',start_time,'结束时间:',end_time,'用时:',end_time-start_time
print('共处理了%s个文件'%(i))
else:
start_time=time.time()
change_ext(lj)
end_time=time.time()
print '开始时间:',start_time,'结束时间:',end_time,'用时:',end_time-start_time
print('共处理了%s个图片'%(i))

python文件_改名2的更多相关文章

  1. python文件_批量改名

    #! /usr/bin/env python #coding=gbk #文件操作实例--将文件夹下所有图片名称加上'_test' import re,os,time #str.split(path) ...

  2. python文件_目录

    #! /usr/bin/env python #coding=gbk import os import time #设置文件的默认路径,当指定的目录不存在时,引发异常:WindowsError:[er ...

  3. python文件批量改名

    python对文件进行批量改名用到的是os模块中的listdir方法和rename方法. os.listdir(dir)  :获取指定目录下的所有子目录和文件名 os.rename(原文件名,新文件名 ...

  4. python文件_读取

    1.文件的读取和显示 方法1: f=open(r'G:\2.txt') print f.read() f.close() 方法2: try: t=open(r'G:\2.txt') print t.r ...

  5. python文件_写入

    1.输入的数据写入到一个文本: #这个写入操作不会将原来的数据覆盖掉 n=raw_input('请输入写入的文件数据:') fl2=open('g:/2.txt','a') fl2.write('\n ...

  6. python 文件批量改名重命名 rename

    path = '/Volumes/Seagate/dev/imgs/' os.chdir(path) print('cwd: ', os.getcwd()) for f in os.listdir(' ...

  7. python开发_搜索本地文件信息写入文件

    功能:#在指定的盘符,如D盘,搜索出与用户给定后缀名(如:jpg,png)相关的文件 #然后把搜索出来的信息(相关文件的绝对路径),存放到用户指定的 #文件(如果文件不存在,则建立相应的文件)中 之前 ...

  8. python习题_读写csv格式的文件

    1.读写TXT文件 # *_* coding : UTF-8 *_* # 开发人员 : zfy # 开发时间 :2019/7/7 16:26 # 文件名 : lemon_10_file.PY # 开发 ...

  9. python学习_数据处理编程实例(二)

    在上一节python学习_数据处理编程实例(二)的基础上数据发生了变化,文件中除了学生的成绩外,新增了学生姓名和出生年月的信息,因此将要成变成:分别根据姓名输出每个学生的无重复的前三个最好成绩和出生年 ...

随机推荐

  1. 基类方法的反隐藏 反private 秘籍

    class GoodStudent:private Mentor,private Student { public : using Mentor::GetInfo;   ///------------ ...

  2. C语言之链表————(转载)

    #include <stdio.h>#include <malloc.h>#define LEN sizeof(struct student) /*-------------- ...

  3. DataSet排序

    //排序            if (ds != null && ds.Tables.Count > 0)            {                DataVi ...

  4. JAVA_build_ant_FixCRLF

    Description Adjusts a text file to local conventions. The set of files to be adjusted can be refined ...

  5. Android面试题(文章内容来自他人博客)

    腾讯面试题 1.int a = 1; int result = a+++3<<2; 2.int a = 2; int result = (a++ > 2)?(++a):(a+=3); ...

  6. 编写Swift代码的其他工具

    Swift程序不能在Windows其他平台编译和运行,有人提供了一个网站swiftstub.com,左栏是代码编辑窗口,右栏是运行结果窗口.可以在任何平台下编译和运行Swift程序.

  7. Codeforces 159D Palindrome pairs

    http://codeforces.com/problemset/problem/159/D 题目大意: 给出一个字符串,求取这个字符串中互相不覆盖的两个回文子串的对数. 思路:num[i]代表左端点 ...

  8. Qt error:QtThese QT version are inaccessible

    安装完Qt Add-in 打开VS2013的时候出现标题错误. QTDIR 需要设置成Qt安装目录下的vc,这个vc目录下包含include,lib,bin等文件夹.或者是在Qt Option里面设置 ...

  9. 【剑指offer】面试题39:二叉树的深度

    题目: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 思路: 根的深度=MAX(左子树深度,右子树深度)+1; Code: ...

  10. POJ1061青蛙的约会(扩展欧几里德算法)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 102239   Accepted: 19781 Descript ...