Does “seek” write zero bytes when used on a newly created binary file?
up vote
1
down vote
favorite
I noticed, that when I do something like this:
with open('testfile', 'wb') as fl:
fl.seek(2048*512)
fl.write(b'aaaaa')
Regardless of my python version, the hexdump of the resulting file will have zero bytes in the first portion of the testfile
:
$ hexdump -C testfile
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00100000 61 61 61 61 61 |aaaaa|
00100005
Is that guaranteed, intended behavior that can be counted on across operating systems? If so, where is that fact documented?
python file-io
add a comment |
up vote
1
down vote
favorite
I noticed, that when I do something like this:
with open('testfile', 'wb') as fl:
fl.seek(2048*512)
fl.write(b'aaaaa')
Regardless of my python version, the hexdump of the resulting file will have zero bytes in the first portion of the testfile
:
$ hexdump -C testfile
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00100000 61 61 61 61 61 |aaaaa|
00100005
Is that guaranteed, intended behavior that can be counted on across operating systems? If so, where is that fact documented?
python file-io
I can't find its behavior in this scenario documented anywhere, so wouldn't count on it.
– martineau
Nov 20 at 1:57
1
"A write operation increases the size of the file to the file pointer position plus the size of the buffer written, which results in the intervening bytes uninitialized." to quote MSDN forSetFilePointer
(which is probably used on Windows to implement seek). docs.microsoft.com/en-us/windows/desktop/api/fileapi/…
– C. Yduqoli
Nov 20 at 2:03
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I noticed, that when I do something like this:
with open('testfile', 'wb') as fl:
fl.seek(2048*512)
fl.write(b'aaaaa')
Regardless of my python version, the hexdump of the resulting file will have zero bytes in the first portion of the testfile
:
$ hexdump -C testfile
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00100000 61 61 61 61 61 |aaaaa|
00100005
Is that guaranteed, intended behavior that can be counted on across operating systems? If so, where is that fact documented?
python file-io
I noticed, that when I do something like this:
with open('testfile', 'wb') as fl:
fl.seek(2048*512)
fl.write(b'aaaaa')
Regardless of my python version, the hexdump of the resulting file will have zero bytes in the first portion of the testfile
:
$ hexdump -C testfile
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00100000 61 61 61 61 61 |aaaaa|
00100005
Is that guaranteed, intended behavior that can be counted on across operating systems? If so, where is that fact documented?
python file-io
python file-io
edited Nov 20 at 1:43
![](https://i.stack.imgur.com/VjcbL.jpg?s=32&g=1)
![](https://i.stack.imgur.com/VjcbL.jpg?s=32&g=1)
martineau
65.1k987176
65.1k987176
asked Nov 20 at 0:55
con-f-use
1,04411838
1,04411838
I can't find its behavior in this scenario documented anywhere, so wouldn't count on it.
– martineau
Nov 20 at 1:57
1
"A write operation increases the size of the file to the file pointer position plus the size of the buffer written, which results in the intervening bytes uninitialized." to quote MSDN forSetFilePointer
(which is probably used on Windows to implement seek). docs.microsoft.com/en-us/windows/desktop/api/fileapi/…
– C. Yduqoli
Nov 20 at 2:03
add a comment |
I can't find its behavior in this scenario documented anywhere, so wouldn't count on it.
– martineau
Nov 20 at 1:57
1
"A write operation increases the size of the file to the file pointer position plus the size of the buffer written, which results in the intervening bytes uninitialized." to quote MSDN forSetFilePointer
(which is probably used on Windows to implement seek). docs.microsoft.com/en-us/windows/desktop/api/fileapi/…
– C. Yduqoli
Nov 20 at 2:03
I can't find its behavior in this scenario documented anywhere, so wouldn't count on it.
– martineau
Nov 20 at 1:57
I can't find its behavior in this scenario documented anywhere, so wouldn't count on it.
– martineau
Nov 20 at 1:57
1
1
"A write operation increases the size of the file to the file pointer position plus the size of the buffer written, which results in the intervening bytes uninitialized." to quote MSDN for
SetFilePointer
(which is probably used on Windows to implement seek). docs.microsoft.com/en-us/windows/desktop/api/fileapi/…– C. Yduqoli
Nov 20 at 2:03
"A write operation increases the size of the file to the file pointer position plus the size of the buffer written, which results in the intervening bytes uninitialized." to quote MSDN for
SetFilePointer
(which is probably used on Windows to implement seek). docs.microsoft.com/en-us/windows/desktop/api/fileapi/…– C. Yduqoli
Nov 20 at 2:03
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Is [zeroed out areas] intended behavior that can be counted on across operating systems? If so, where is that fact documented?
Across operating systems? Probably not: if Python is ported to some ancient OSes, those writes might either not produce a gap at all (ignoring the seek), or fail. However, modern systems all support sparse files, or at least fake them to the point of making the above work just fine and behave that way. If someone doing such a backport cares, they could add a faking layer.
You're probably safe if you rely on this. Just don't assume that the holes will stay holes: some systems may fill them in when restoring from backup or migrating files across a cluster or whatever. If you seek a few terabytes in and write one byte, the file might take only a little space until the fill-in point, if and when it ever occurs.
add a comment |
up vote
1
down vote
A binary file has a pointer that indicates the file position at which the next read or write operation will take place. Position is counted in terms of bytes. Read and write operations always move the pointer so it's at the end of whatever was just read or written.
Seeking to a position past the end of the file results in the file size being increased as needed, with the new bytes filled with 0. In the code above, you're creating a new file and using seek
incrementing filepointer's (In python, there are no pointers, so this is stream position
) start position. This will add 0's.
In Binary files, the current stream position
is the bytes offset from the start of the file. If you increase stream position, all previous positions will be filled with 0 in a binary file.
Is this across operating systems; probably YES for all latest or LTS OSes. Might not be promising in some legacy systems.
Stream Position Documentation -https://docs.python.org/3/library/io.html#binary-i-o
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Is [zeroed out areas] intended behavior that can be counted on across operating systems? If so, where is that fact documented?
Across operating systems? Probably not: if Python is ported to some ancient OSes, those writes might either not produce a gap at all (ignoring the seek), or fail. However, modern systems all support sparse files, or at least fake them to the point of making the above work just fine and behave that way. If someone doing such a backport cares, they could add a faking layer.
You're probably safe if you rely on this. Just don't assume that the holes will stay holes: some systems may fill them in when restoring from backup or migrating files across a cluster or whatever. If you seek a few terabytes in and write one byte, the file might take only a little space until the fill-in point, if and when it ever occurs.
add a comment |
up vote
1
down vote
Is [zeroed out areas] intended behavior that can be counted on across operating systems? If so, where is that fact documented?
Across operating systems? Probably not: if Python is ported to some ancient OSes, those writes might either not produce a gap at all (ignoring the seek), or fail. However, modern systems all support sparse files, or at least fake them to the point of making the above work just fine and behave that way. If someone doing such a backport cares, they could add a faking layer.
You're probably safe if you rely on this. Just don't assume that the holes will stay holes: some systems may fill them in when restoring from backup or migrating files across a cluster or whatever. If you seek a few terabytes in and write one byte, the file might take only a little space until the fill-in point, if and when it ever occurs.
add a comment |
up vote
1
down vote
up vote
1
down vote
Is [zeroed out areas] intended behavior that can be counted on across operating systems? If so, where is that fact documented?
Across operating systems? Probably not: if Python is ported to some ancient OSes, those writes might either not produce a gap at all (ignoring the seek), or fail. However, modern systems all support sparse files, or at least fake them to the point of making the above work just fine and behave that way. If someone doing such a backport cares, they could add a faking layer.
You're probably safe if you rely on this. Just don't assume that the holes will stay holes: some systems may fill them in when restoring from backup or migrating files across a cluster or whatever. If you seek a few terabytes in and write one byte, the file might take only a little space until the fill-in point, if and when it ever occurs.
Is [zeroed out areas] intended behavior that can be counted on across operating systems? If so, where is that fact documented?
Across operating systems? Probably not: if Python is ported to some ancient OSes, those writes might either not produce a gap at all (ignoring the seek), or fail. However, modern systems all support sparse files, or at least fake them to the point of making the above work just fine and behave that way. If someone doing such a backport cares, they could add a faking layer.
You're probably safe if you rely on this. Just don't assume that the holes will stay holes: some systems may fill them in when restoring from backup or migrating files across a cluster or whatever. If you seek a few terabytes in and write one byte, the file might take only a little space until the fill-in point, if and when it ever occurs.
answered Nov 20 at 1:23
torek
179k17229308
179k17229308
add a comment |
add a comment |
up vote
1
down vote
A binary file has a pointer that indicates the file position at which the next read or write operation will take place. Position is counted in terms of bytes. Read and write operations always move the pointer so it's at the end of whatever was just read or written.
Seeking to a position past the end of the file results in the file size being increased as needed, with the new bytes filled with 0. In the code above, you're creating a new file and using seek
incrementing filepointer's (In python, there are no pointers, so this is stream position
) start position. This will add 0's.
In Binary files, the current stream position
is the bytes offset from the start of the file. If you increase stream position, all previous positions will be filled with 0 in a binary file.
Is this across operating systems; probably YES for all latest or LTS OSes. Might not be promising in some legacy systems.
Stream Position Documentation -https://docs.python.org/3/library/io.html#binary-i-o
add a comment |
up vote
1
down vote
A binary file has a pointer that indicates the file position at which the next read or write operation will take place. Position is counted in terms of bytes. Read and write operations always move the pointer so it's at the end of whatever was just read or written.
Seeking to a position past the end of the file results in the file size being increased as needed, with the new bytes filled with 0. In the code above, you're creating a new file and using seek
incrementing filepointer's (In python, there are no pointers, so this is stream position
) start position. This will add 0's.
In Binary files, the current stream position
is the bytes offset from the start of the file. If you increase stream position, all previous positions will be filled with 0 in a binary file.
Is this across operating systems; probably YES for all latest or LTS OSes. Might not be promising in some legacy systems.
Stream Position Documentation -https://docs.python.org/3/library/io.html#binary-i-o
add a comment |
up vote
1
down vote
up vote
1
down vote
A binary file has a pointer that indicates the file position at which the next read or write operation will take place. Position is counted in terms of bytes. Read and write operations always move the pointer so it's at the end of whatever was just read or written.
Seeking to a position past the end of the file results in the file size being increased as needed, with the new bytes filled with 0. In the code above, you're creating a new file and using seek
incrementing filepointer's (In python, there are no pointers, so this is stream position
) start position. This will add 0's.
In Binary files, the current stream position
is the bytes offset from the start of the file. If you increase stream position, all previous positions will be filled with 0 in a binary file.
Is this across operating systems; probably YES for all latest or LTS OSes. Might not be promising in some legacy systems.
Stream Position Documentation -https://docs.python.org/3/library/io.html#binary-i-o
A binary file has a pointer that indicates the file position at which the next read or write operation will take place. Position is counted in terms of bytes. Read and write operations always move the pointer so it's at the end of whatever was just read or written.
Seeking to a position past the end of the file results in the file size being increased as needed, with the new bytes filled with 0. In the code above, you're creating a new file and using seek
incrementing filepointer's (In python, there are no pointers, so this is stream position
) start position. This will add 0's.
In Binary files, the current stream position
is the bytes offset from the start of the file. If you increase stream position, all previous positions will be filled with 0 in a binary file.
Is this across operating systems; probably YES for all latest or LTS OSes. Might not be promising in some legacy systems.
Stream Position Documentation -https://docs.python.org/3/library/io.html#binary-i-o
answered Nov 20 at 1:34
![](https://lh6.googleusercontent.com/-McrhoAjt2tk/AAAAAAAAAAI/AAAAAAAAAA0/PQRkg7lAC5k/photo.jpg?sz=32)
![](https://lh6.googleusercontent.com/-McrhoAjt2tk/AAAAAAAAAAI/AAAAAAAAAA0/PQRkg7lAC5k/photo.jpg?sz=32)
Pruthvi Kumar
57718
57718
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384736%2fdoes-seek-write-zero-bytes-when-used-on-a-newly-created-binary-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I can't find its behavior in this scenario documented anywhere, so wouldn't count on it.
– martineau
Nov 20 at 1:57
1
"A write operation increases the size of the file to the file pointer position plus the size of the buffer written, which results in the intervening bytes uninitialized." to quote MSDN for
SetFilePointer
(which is probably used on Windows to implement seek). docs.microsoft.com/en-us/windows/desktop/api/fileapi/…– C. Yduqoli
Nov 20 at 2:03