Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Python3: what does it mean to f.read(4096)?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
suttiwit
Posts: 11
Joined: 2014-11-30 02:57

Python3: what does it mean to f.read(4096)?

#1 Post by suttiwit »

Basically, I would like to read a binary file in blocks of 4096 bytes. And I know to myself that using f.read(X) makes a system call which would be slow. And since i can't read the whole file and store it in binary because those files are gigabytes in size: I decide to read in blocks of 4096.

But now, I am stumped: I want to know what it means to f.read(4096).... Does it really call a system call once to get the successive 4096 bytes from the file or does it actually call a system call that gets 1 byte from the file 4096 times?

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Python3: what does it mean to f.read(4096)?

#2 Post by GarryRicketson »


tomazzi
Posts: 730
Joined: 2013-08-02 21:33

Re: Python3: what does it mean to f.read(4096)?

#3 Post by tomazzi »

suttiwit wrote:Basically, I would like to read a binary file in blocks of 4096 bytes. And I know to myself that using f.read(X) makes a system call which would be slow.
It looks like You have completely no idea of what You're actually want to do...
It seems that You've heard *something* about memory pages, but unfortunately You have completely no idea where the 4Kb buffer size comes from... just a disaster... (it's a false assumption that the memory page is 4096 bytes wide)

You should know (and this is the actual answer) that there's a technique, which was developed far before the UNIX started to exist, called: mmap - which allows to access any kind of data stored in a file just like if they were an array of bytes - or, in more advanced cases - a structure...

Regards ;)
Odi profanum vulgus

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Python3: what does it mean to f.read(4096)?

#4 Post by GarryRicketson »

For those that do not know what "mmap" is :

http://man7.org/linux/man-pages/man2/mmap.2.html
and
https://en.wikipedia.org/wiki/Mmap
Also

Code: Select all

$ man Mmap
------------------
More, on how to to read a binary file in blocks of 4096 bytes

Post Reply