Python HW 1번문제
Sorce Bank 2010. 5. 6. 00:05
import os.path import glob import cStringIO import os import sys if(len(sys.argv)!=2): print("Usage : python <Script name> <Dir Name>") sys.exit(1) files={} id=0 def countWord(filename): in_file = open(filename) text = in_file.read() in_file.close() strIO = cStringIO.StringIO(text) filelines = strIO.readlines() numOfWd=0 numOfLn=0 word=[] for line in filelines: word+=line.split() numOfLn+=1 for w in word: numOfWd+=1 return numOfWd, numOfLn def searchDir(dirName): fileList = glob.glob(dirName) for fullFile in fileList: if os.path.isdir(fullFile): searchDir(fullFile+"/*") else: d_name = os.path.dirname(fullFile) f_name = os.path.basename(fullFile) words,lines = countWord(fullFile) value=(d_name,f_name,lines,words) global id id+=1 files[id]=value searchDir(sys.argv[1]+"/*") f_keys = files.keys() for a in f_keys: print "id", a info = files[a] print "Path : ", info[0] print "File name : ", info[1] print "Number of lines :", info[2] print "Number of Words :",info[3]
'Sorce Bank' 카테고리의 다른 글
Python BST + AVL (0) | 2010.05.08 |
---|---|
Python HW 2번문제 (0) | 2010.05.06 |
그래픽스 실습 4.27 (0) | 2010.04.27 |
파이썬 c API 하다 만거 (0) | 2010.04.15 |
네트워크 홈워크 1 (0) | 2010.03.25 |