-1
import glob

files = glob.glob('sample/*.txt')

books = []

for file in files:
    with open(file, 'r', encoding='utf8') as infile:
        books.append(infile.read().replace('\n', ' '))
        
books[0][:800]

I used this code to read in the data but I'm having trouble doing anything with this data.

B Remmelzwaal
  • 1,581
  • 2
  • 4
  • 11
Ssangkal
  • 1
  • 2
  • You could use `max(books, key=len)` to get the file with the most characters. – B Remmelzwaal Feb 28 '23 at 01:24
  • Why would you store the entire book, if what you're interested in is the character count? Read in one file, count the number of characters and call that `count` or something, then save that along with the filename (e.g. in a dict in your `books` list. Now your problem has been reduced to "how do I find the entry with the highest `count` in my list?". And that's a problem with a million answers on both SO and the web already =) – Mike 'Pomax' Kamermans Feb 28 '23 at 01:29
  • 1
    Why would you need python at all? Seems like you can just run the `ls` or `dir` command, and sort on file size... – John Gordon Feb 28 '23 at 02:13

1 Answers1

0

You can use the max function on the list of file names and get the content size as the ordering key:

import glob

files = glob.glob('*.txt')

largestFile = max(files,key=lambda name:len(open(name).read()))

print(largestFile) # name of the largest file
    
Alain T.
  • 40,517
  • 4
  • 31
  • 51