R
Rob Schneider
Yep, there's the problem! See my previous post for details. Change this to:
f.close()
and you should be sorted.
ChrisA
Slapping forehead ... hard. Thanks!
Yep, there's the problem! See my previous post for details. Change this to:
f.close()
and you should be sorted.
ChrisA
Slapping forehead ... hard. Thanks!
Rob Schneider said:Source (correct one) is 47,970 bytes. Target after copy of 45,056 bytes.
I've tried changing what gets written to change the file size. It is usually
this sort of difference.
The file system is Mac OS Extended Journaled (default as out of the box).
Hold it one moment... You're not actually calling close. The file's
still open. Is that a copy/paste problem, or is that your actual code?
In Python, a function call ALWAYS has parentheses after it. Evaluating
a function's name like that returns the function (or method) object,
which you then do nothing with. (You could assign it someplace, for
instance, and call it later.) Try adding empty parens:
f.close()
and see if that solves the problem. Alternatively, look into the
'with' statement and the block syntax that it can give to I/O
operations.
Steven D'Aprano said:But note that "done" in this case means "the file system thinks it is
done", not *actually* done. Hard drives, especially the cheaper ones,
lie. They can say the file is written when in fact the data is still in
the hard drive's internal cache and not written to the disk platter.
Also, in my experience, hardware RAID controllers will eat your data, and
then your brains when you try to diagnose the problem.
I would consider the chance that the disk may be faulty, or the file
system is corrupt. Does the problem go away if you write to a different
file system or a different disk?
Cameron Simpson said:45046 is exactly 11 * 4096. I'd say your I/O is using 4KB blocks,
and the last partial block (to make it up to 47970) didn't get
written (at the OS level).
Rob Schneider said:f.close
Don't you model this as a non-blocking operation inBut note that "done" in this case means "the file system thinks it is
done", not *actually* done. Hard drives, especially the cheaper ones,
lie. They can say the file is written when in fact the data is still in
the hard drive's internal cache and not written to the disk platter.
Also, in my experience, hardware RAID controllers will eat your data, and
then your brains when you try to diagnose the problem.
I would consider the chance that the disk may be faulty, or the file
system is corrupt. Does the problem go away if you write to a different
file system or a different disk?
But note that "done" in this case means "the file system thinks it is
done", not *actually* done. Hard drives, especially the cheaper ones,
lie. They can say the file is written when in fact the data is still in
the hard drive's internal cache and not written to the disk platter.
Also, in my experience, hardware RAID controllers will eat your data, and
then your brains when you try to diagnose the problem.
But most of the time, there's no point. If you actually care about what
happens in the event of an unclean shutdown, you typically also need to
sync the directory, otherwise the file's contents will get sync'd but the
file's very existence might not be.
None of which is likely to be relevant here,
If you want to wait for the data written to be written to the physical
disk (in order to obtain specific behaviour with respect to an unclean
shutdown), use f.flush() followed by os.fsync(f.fileno()).
If only it were that simple. It has been documented that some disks will
lie, even when told to sync. When I say "some", I mean *most*. There's
probably nothing you can do about it, apart from not using that model or
brand of disk, so you have to just live with the risk.
USB sticks are especially nasty. I've got quite a few USB thumb drives
where the "write" light keeps flickering for anything up to five minutes
after the OS reports that the drive has been unmounted and is safe to
unplug. I corrupted the data on these quite a few times until I noticed
the light. And let's not even mention the drives that have no light at
all...
But my favourite example of lying hard drives of all time is this:
http://blog.jitbit.com/2011/04/chinese-magic-drive.html
I want one of those!
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.