Python Forum
What is meant by "truncates the file" RE file.open(w+b)?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is meant by "truncates the file" RE file.open(w+b)?
#6
(Sep-01-2020, 07:46 PM)MysticLord Wrote: When open("file,"w") is chosen, does it retain the file length and zero out everything, or does it set the file length to zero as well?

It's opening the file with O_TRUNC as an option to the call. Your OS should have documentation on how that behaves. On linux it's in the open man page. There it states that the length is truncated to 0. So no data is explicitly wiped. It's just the length is set to zero. Attempts to read past that point will generate EOF.

Quote:If I wanted to write to a file in "insert mode", I'd need to open it as read elsewhere, copy everything up to my write location into a separate new write-flagged file, append my changes, and then copy the rest of the read-flagged file into the write-flagged file. Is this correct?

Feels like an odd choice. Where can I find more detailed information, is there a good book on Python I/O you recommend?

This has nothing to do with python, per se. This is how the file interface works on all currently popular filesystems that I can think of on unix/linux/mac/windows. A file offset (like the 4096'th byte of a file) is stored at a particular location on disk. The operating system gives you easy ability to read or write at a particular position, since it doesn't affect the rest of the file.

The filesystem and the file interfaces have no tools to allow "insertion" (vs overwriting) of data into the middle of the file. It's possible to make a filesystem where inserting a chunk is easy (extent-based filesystems could do it cheaply), but that's not how they're typically implemented. Instead, changing the offset of chunk means rewriting it at the new offset. A 1GB file with a character added to the beginning must have the whole file written again.

It's basically like being given a pile of typewritten sheets and asked to change a sentence. If the replacement fits exactly, you can use white-out and write it over the old one. If the replacement is larger, there's no way to "shift the rest down" without retyping/reprinting everything.
Reply


Messages In This Thread
RE: What is meant by "truncates the file" RE file.open(w+b)? - by bowlofred - Sep-01-2020, 08:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write variable in a python file then import it in another python file? tatahuft 4 955 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  Trying to open depracated joblib file mckennamason 0 744 Sep-19-2024, 03:30 PM
Last Post: mckennamason
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,085 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 977 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,468 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Open/save file on Android frohr 0 1,141 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 7,686 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 1,941 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can i combine these two functions so i only open the file once? cubangt 4 1,984 Aug-14-2023, 05:04 PM
Last Post: snippsat
  How can I change the uuid name of a file to his original file? MaddoxMB 2 2,152 Jul-17-2023, 10:15 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020