Python批量修改文件後綴名,文件內…


#!/usr/bin/python
# -*- coding:utf-8 -*-
import os
def BatRename(path):
    filelist = [item for item in os.listdir(path) if os.path.isfile(os.path.join(path,item)) \
                and os.path.splitext(item)[1] == '.py']
    for hfile in filelist:
        os.rename(os.path.join(path, hfile), os.path.join(path, ''.join([os.path.splitext(hfile)[0], '.txt'])))  
filedir = "D:\\test\\10.13\\"
addtext = 'heihei'
for name in os.listdir(filedir):
    name_path = filedir+name
    f = file(name_path,'a')
    f.write(addtext)
    f.close()
filelist=[item for item in ***]遍歷列表
os.listdir(path)                    列出path中所有的文件和目錄,不包括子目錄中的內容
os.path.isfile(X)                   判斷X是否爲文件
os.path.join()                      將多個路徑組合返回,第一個路徑之前的參數將被忽略
os.path.splitext                   分離文件名與擴展名;默認返回(fname,fextension)元組,可做分片操作

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章