Chinaunix

标题: python 修改文件名 [打印本页]

作者: 雨也潇潇88    时间: 2011-01-07 14:05
标题: python 修改文件名
我想跟一个文件中含有Z的全部用21替代,应该用什么函数啊?有知道的朋友吗?谢谢
作者: iamlike    时间: 2011-01-07 14:18
  1. os.rename(old,new)
复制代码

作者: 雨也潇潇88    时间: 2011-01-07 14:44
回复 2# iamlike

import os,os.path  
import shutil,string  
#coding=utf-8
#coding=gbk
ws = os.getcwd()
dir =ws
for i in os.listdir(dir):  
      old = os.path.basename(i )
      newfile =i.replace('*z*','11')
      newfile =i.replace('*s*','01')
      newfile =i.replace('*x*','21')
      os.rename(old,newfile)
我是这样写的,但是运行的时候不对

  我的文件名是这样的我想将第一个出现的Z  平均气温0010z.shx改成平均气温001011.shx
作者: nagaregawa    时间: 2011-01-07 18:25
i.replace('*z*','11',1)
作者: 雨也潇潇88    时间: 2011-01-08 14:21
回复 4# nagaregawa


    多谢。已经解决了哈!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2