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)?
#1
https://docs.python.org/3/library/functions.html#open

Quote:The default mode is 'r' (open for reading text, synonym of 'rt'). Modes 'w+' and 'w+b' open and truncate the file. Modes 'r+' and 'r+b' open the file with no truncation.
Which do I use if I want to seek within a file in binary mode, write to it in an overwrite rather than insertion mode, and close it?

edit

Also how would I convert a string "7F" or "1A" from a string representation of a hexadecimal number into a decimal number?

double edit

I think this should work for the last question.
int("7F",16)
int("1A",16)
int(lineList[3][n:n+1],16)
Here's the code for my script.
import sys,io

csvName = sys.argv[1]
with open(csvName, "r") as f:
    line = f.readline()
    while line != '':
        lineList = line.split(',')
        with open(lineList[0],"w+b") as writeFile:
            #waiting on python forum reply for above
            writeFile.seek(int(lineList[1]))
            #next do a loop where you grab lineList[3][n:n+1],
            #convert it from a hex string to an int,
            #and write it.
            #maybe int(lineList[3][n:n+1],16)
        line = f.readline()
        
        #file.seek(36,0)
        #address = file.read(1)[0]
        #address = ((file.read(1)[0] << 8) | address)
        #address = ((file.read(1)[0] << 16) | address)
        #address = ((file.read(1)[0] << 24) | address)
        #print(str(address))
        #print(hex(address))



#open csv file
#while (readfirstline() != EOF)
#{
#  split line along commas
#  open file in first element in array or list as write binary
#  seek to addresss in second element in array or list
#  convert third line into format writable to binary file
#  write third line
#  close file
#}

#just operating on one file, don't need this for loop or wildcards
Reply
#2
There is no "insertion" mode. There's no way to put something in the middle of a file and have other things rearranged. So "r+" or "r+b" will allow you to seek within the file and overwrite portions.
Reply
#3
The "wb truncates" means that opening a file with "w" (the "b" doesn't matter for this) resets the file to empty. Truncates is not a great choice of words because we are used to "truncate to a value" or "truncate to a precision". Here it means "truncate to 0 bytes".

There is only one place you should ever write to a file, after the end. You can do this with an empty file, or with a file that contains values. You need to be really careful when trying to write anywhere else.
Reply
#4
r+b is how I write to a file without zeroing the part between the end of my writes and the end of the file, or any other parts, correct?

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?

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?
Reply
#5
As bowlofred says, there is no such thing as "insert mode". If you want to "insert" something in a file you need to rebuild the file. This is not a "Python" thing. This is a "how all files work" thing.

As for your other questions, they are best answered by doing some experiments. Open an existing file using "w" and close it immediately. What happened to the file size?
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 279 Jan-24-2024, 06:28 PM
Last Post: frohr
  file open "file not found error" shanoger 8 942 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Need to replace a string with a file (HTML file) tester_V 1 699 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 805 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 868 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,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  I cannot able open a file in python ? ted 5 3,048 Feb-11-2023, 02:38 AM
Last Post: ted
  testing an open file Skaperen 7 1,300 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,062 Dec-15-2022, 04:32 PM
Last Post: Larz60+
Photo Making Zip file of a file and Directory Nasir 2 984 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