return tot generator expression can have the same effect (though the instructor might take off points for excessive magic!)def countlines(name)return sum(+ for line in open(name)def countchars(name)return sum(len(linefor line in open(name)on unixyou can verify your output with wc commandon windowsright-click on your file to view its properties note that your script may report fewer characters than windows does--for portabilitypython converts windows \ \ line-end markers to \nthereby dropping byte (characterper line to match byte counts with windows exactlyyou must open in binary mode ('rb')or add the number of bytes corresponding to the number of lines see and for more on end-of-line translations in text files the "ambitiouspart of this exercise (passing in file object so you only open the file once)will require you to use the seek method of the built-in file object it works like ' fseek call (and may call it behind the scenes)seek resets the current position in the file to passed-in offset after seekfuture input/output operations are relative to the new position to rewind to the start of file without closing and reopening itcall file seek( )the file read methods all pick up at the current position in the fileso you need to rewind to reread here' what this tweak would look likedef countlines(file)file seek( return len(file readlines()rewind to start of file def countchars(file)file seek( return len(file read()ditto (rewind if neededdef test(name)file open(namereturn countlines(file)countchars(filepass file object open file only once import mymod mymod test("mymod py"( from/from here' the from partreplace with countchars to do the restpython from mymod import countchars("mymod py" __main__ if you code it properlythis file works in either mode--program run or module importdef countlines(name)file open(name appendix dsolutions to end-of-part exercises