[問題] word count in files

看板Python作者 (獨在異鄉為異客)時間17年前 (2008/02/25 05:21), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
I'm trying to write a command that print the largest file in the current working directory. I'm able to print all the files and their wordcount, but I don't know how to print just the largest one. Here's what I have: import os cwd = os.getcwd() for file in os.listdir(cwd): if os.path.isdir(file) == False: if file[0] != '.': word = open(file, 'r').read().split() wordcount = len(word) I tried to use range function to do the work, but it didn't work. I wrote like this (following what I had above): for x in range(wordcount): if x in range(wordcount - 1) == False: print x When I ran it, nothing happened. Can anyone tell me what's wrong? Or is there a better way to do it? My logic is that if the number is larger than the max number -1, it should be the largest number. Thanks in advance for any help! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 69.143.181.102
文章代碼(AID): #17mT_QsQ (Python)
討論串 (同標題文章)
文章代碼(AID): #17mT_QsQ (Python)