python - How does len function actually work for files? -
python - How does len function actually work for files? -
the python docs say: homecoming length (the number of items) of object. argument may sequence (string, tuple or list) or mapping (dictionary).
code:
from sys import argv script, from_file = argv input = open(from_file) indata = input.read() print "the input file %d bytes long" % len(indata)
contents of file: 1 2 three
upon running simple programme output: input file 14 bytes long
qutestion:
i don't understand, if file has written in 11 characters(one 2 three) how can len homecoming me 14 bytes , not 11?(what's bytes way?) in python interpreter if type s = "one 2 three" , len(s) 13, confused.
"one 2 three" indeed 13 chars (11 letters + 2 spaces).
>>> open("file.txt", 'w').write("one 2 three") >>> len(open("file.txt").read()) 13
most have char endline, explains 14.
python function
Comments
Post a Comment