python文件操作实例

把目录 E: 下面所有 后缀名为 .py 的 文件复制到 E:PyLearn

#coding:utf-8

import os
import shutil

def getfile(srcDir,dstDir,extName):
    filelist = os.listdir(srcDir)
    for tgFile in filelist:
        tgFilePath = os.path.join(srcDir,tgFile)
        if os.path.isfile(tgFilePath) == True:
            tgFile_tuple = os.path.splitext(tgFilePath)
            if tgFile_tuple[1] == extName:
                shutil.copy(tgFilePath,dstDir)

srcDir = 'e:' + os.sep
dstDir = 'e:' + os.sep + 'LearnPy'
extName = '.py'
getfile(srcDir,dstDir,extName)
原文地址:https://www.cnblogs.com/yuuwee/p/5691944.html