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


Messages In This Thread
What is meant by "truncates the file" RE file.open(w+b)? - by MysticLord - Sep-01-2020, 10:44 AM

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