python之学习类:python修改文件名
下面是对某文件夹下多个文件下指定文件换名字的实例(为了换名字,因为懒得手动改,折腾了一会搞出来的)
原理很简单,换文件名的话指定path就行 ,具体的自己看吧,仅供参考!
#-*- coding:utf-8 -*-
import os,sys
#=======================================
##对多个文件夹下的文件(夹)进行处理
#=======================================
def getdirNames(path):
dirNames = os.listdir(path)
return dirNames
def isDir(dirNames,path):
for item in dirNames:
dir = os.path.join(path,item)
if(os.path.isdir(dir)):
dirAddress.append(dir)
file = getdirNames(dir)
isDir(file,dir)
else:
if 'png' in item:
fileAddress.append(dir)
return fileAddress
#=======================================
##对某个文件夹下面的文件进行处理
#=======================================
def fil(filename):
mapdir = filename[-1:]
mapname = filename[:-2].replace('0','')
changename = mapname[::-1].zfill(8)[::-1]
rename = changename+'_'+mapdir
return rename
if __name__ == '__main__':
#此处添加需要转换名字的文件夹路径(文件夹下面可以有多个文件夹),【例如:在此我将“0103020202_0.png”转换成“13222000_0.jpg:】
path0 = r'F:\python\20100412\game\data\images\Object\World\Road'
dirAddress = []
fileAddress = []
dirNames = getdirNames(path0)
isDir(dirNames,path0)
print fileAddress
for i in range(len(fileAddress)):
&
相关文档:
python字典排序
1、
准备知识:
在python里,字典dictionary是内置的数据类型,是个无序的存储结构,每一元素是key-value对:
如:dict = {‘username’:‘password’,‘database’:‘master’},其中‘username’和& ......
#直接选择排序
def SelectSort(mylist):
size = len(mylist)
i = 0
for i in range(0, size):
k = i
for j in range(i + 1, size):
if mylist[j] < mylist[k]:
k = j
if k != i:
tmp = mylist[i]
......
#!/usr/bin/env python
#coding=utf-8
def buildConnectionString(params):
return ":".join(["%s=%s" %(k, v) for k, v in params.items()])
if __name__ == "__main__":
myParams = {"server":"mpilgrim", \
&nbs ......
1. Building an Application with PyGTK and Glade
2. Creating a GUI using PyGTK and Glade
3. A Beginner's Guide to Using pyGTK and Glade
4. Is there a walkthrough on getting PyGTK2 and libglade2 to work on win32 ......
时常见到一些好的Python模块,如果不随时记下,等用的时候又是一阵乱翻,好在Python引用一个模块极其方便,OK,废话少说:
1. decimal
标准模块,2.4引入,用于浮点数的精确表示:
>>> 0.1 + 0.1 + 0.1 - 0.3
5.5511151231257827e-17
>>> from decimal import Decimal
>>> def _(x): retu ......