[問題] word count in files
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
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 1 之 3 篇):
Python 近期熱門文章
PTT數位生活區 即時熱門文章