Python遍历目录,操作文件

#coding=utf-8
import os

def GetFileList(dir, fileList):
    newDir = dir
    files = os.listdir(dir);
    for file in files:
        newDir = os.path.join(dir,file)
        if os.path.isfile(newDir):
            fileList.append(newDir)
        elif os.path.isdir(newDir):
            GetFileList(newDir,fileList)
    return fileList

dirpath="/public/data/"
outpath="/public/home/sun/data/"
items = GetFileList(dirpath,[])

for item in items:
    if item.endswith(".txt"):
        name = item.split('/')[-1]    #split列表最后一个元素
        out_cmd = '命令行'
        os.popen(out_cmd)
        print "finished!"

你可能感兴趣的:(Python)