def file_open(filename):
fo=open(filename,'r')
#fo.seek(5)
fo.read(3)
fo.close()
file_open("file_ro.py")
I expect above program to return first 3 bytes from file . But it returns nothing. When I ran these in interactive python command prompt – I get expected output!
While your own answer prints the bytes read, it doesn’t return them, so you won’t be able to use the result somewhere else. Also, there’s room for a few other improvements:
file_openisn’t a good name for the function, since it reads and returns bytes from a file rather than just opening it.fo.read(3)fails. You can use the with statement to solve this issue.The modified code could look something like this:
Usage: