python应用

来源:互联网 发布:心目中最好av作品知乎 编辑:程序博客网 时间:2024/06/05 03:58
域名过滤:从daminFile文件按行读取,将符合规则的域名行记录下来。
import osimport re#input the file pathdaminFile = raw_input('File path:\n')#result file pathresultFile = daminFile+'_rs.txt'fileRead = open(daminFile, 'r')fileWrite = open(resultFile, 'w')#set the ruleregex = ur'^[\w]{1,5}\.(com|net|org)$'#get each linefor eachLine in fileRead:print eachLineif re.match(regex, eachLine):fileWrite.write('%s' % eachLine)fileWrite.close()fileRead.close()


第一次将python用于实际需求:想注册一个域名,到最新过期的域名里挑,每天数十万的记录,人工挑选是不可能的,用python先挑选出长度5位以内,com/net/org 域名。

不算注释和空行,12行代码,python真的很省事。

----------------------------------分割线---------------------------------------

去除项目目中中.svn文件 :

import os,sys,stat,shutildirPath = ''def walk(path):for fileobj in os.listdir(path):if fileobj == '.svn':shutil.rmtree(os.path.join(path, fileobj))else:filepath = path+'//'+fileobjisDir = os.path.isdir(filepath)if isDir:print filepathwalk(filepath)def getInput():dirPath = raw_input('Enter the dir path(eg:"e:/test"):\n')if os.path.isdir(dirPath):walk(dirPath)else:print 'there is not a directory named ' + dirPathgetInput()if __name__=='__main__':getInput()





原创粉丝点击