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
  Open/save file on Android frohr 0 418 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 1,425 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 857 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 992 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 1,066 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,221 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  I cannot able open a file in python ? ted 5 3,838 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,529 Dec-20-2022, 02:19 AM
Last Post: Skaperen
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,218 Dec-15-2022, 04:32 PM
Last Post: Larz60+
Photo Making Zip file of a file and Directory Nasir 2 1,097 Oct-07-2022, 02:01 PM
Last Post: Nasir

Forum Jump:

User Panel Messages

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