python 常用函数(不定时更新)

1.遍历文件夹

 1 import os
 2 def FileList(rootDir):
 3     FL=[]
 4     for lists in os.listdir(rootDir): 
 5         path=os.path.join(rootDir, lists) 
 6         FL.append(path)
 7         
 8         if os.path.isdir(path): 
 9             FileList(path) 
10     return FL

2.时间戳

import datetime

start=datetime.datetime.now()
print start
    。。。
end=datetime.datetime.now()
print end
print 'it costs ',end-start

 3.合并多个TXT,生成新TXT。

import os
def MergeTXT(TXTlist,desTXT):
    f=open(desTXT,'w')
    for i in TXTlist:
        if isTXT(i):
            x = open(i, 'r')
            f.write(x.read())
            x.close()
        else:
            pass
    f.close()

def isTXT(filename):
    d=len(filename)
    e=filename.find('.txt',d-4,d)
    if e<0:
        return 0 #Not TXT
    else:
        return 1 #Is TXT
View Code

 

posted on 2013-12-22 16:58  卡西大人  阅读(188)  评论(0编辑  收藏  举报