Updating value in binary file with Python - python

I'm trying to figure out how to update the data in a binary file using Python.
I'm already comfortable reading and writing complete files using "array", but I'm having trouble with in place editing.
Here's what I've tried:
my_file.seek(100)
my_array = array.array('B')
my_array.append(0)
my_array.tofile(my_file)
Essentially, I want to change the value of the byte at position 100. The above code does update the value, but then truncates the rest of the file. I want to be able to change the value at position 100, without modifying anything else in the file.
Note that I'm editing multi-gigabyte files, so I don't want to read the entire thing into memory, update memory, and then write back out to disk.

According to the documentation of open(), you should open the file in 'rb+' mode to avoid the truncating behavior.

Are you opening the file in 'r+b' mode?

Related

Get a binary text from a file in python [duplicate]

This question already has answers here:
How to open and read a binary file in Python?
(3 answers)
Closed 1 year ago.
How can I get zeros and ones that make a file? For example, how can I open a file with Python and get the zeros and ones that make it up? And convert those zeros and ones again to a file?
Thanks for your help!
This question is very vague, so I will try and answer some of the possible questions I think you are asking.
How do I open a non-text file in binary mode
Some files need to have the "binary" versions of the files opened. A easier way to think of this would be opening a file in "raw" (binary) vs "plaintext" (text) mode.
Often API's that work with PDF files, and certain other extensions use this to open the files properly since they contain characters that are encoded to be unreadable, and use headers that would garble the files as plain strings.
To do this change the mode of a call to open() to any of these :
rb for reading a binary file. The file pointer is placed at the beginning of the file.
rb+ reading or writing a binary file
wb+ writing a binary file
ab+ Opens a file for both appending and reading in binary. The file pointer is at the end of the file if the file exists. The file opens in the append mode.
For example:
with open("filename.pdf", "rb") as pdf_file:
... # Do things
How do I get the binary values of individual string characters
If you are looking to open a file and get a binary association to a character you can use the ord() function combined with the bin() function:
ord("A") # 65
bin(ord("A")) # '0b1000001'
You can then loop through each character of a file and find a binary representation of each letter this way. This is often used in cryptography, like I did for this project.
If neither of those two solve your issues please clarify what you mean in the original question so I can better address it.

Approaches to loading JSON from file to dict using Python?

Using Python I'm loading JSON from a text file and converting it into a dictionary. I thought of two approaches and wanted to know which would be better.
Originally I open the text file, load the JSON, and then close text file.
import json
// Open file. Load as JSON.
data_file = open(file="fighter_data.txt", mode="r")
fighter_match_data = json.load(data_file)
data_file.close()
Could I instead do the following instead?
import json
// Open file. Load as JSON.
fighter_match_data = json.load(open(file="fighter_data.txt", mode="r"))
Would I still need to close the file? If so, how? If not, does Python close the file automatically?
Personally wouldn't do either. Best practice for opening files generally is to use with.
with open(file="fighter_data.txt", mode="r") as data_file:
fighter_match_data = json.load(data_file)
That way it automatically closes when you're out of the with statement. It's shorter than the first, and if it throws an error (say, there's an error parsing the json), it'll still close it.
Regarding your actual question, on needing to close the file in your one liner.
From what I understand about file handling and garbage collection, if you're using CPython, since the file isn't referenced anymore it "should" be closed straight away by the garbage collector. However, relying on garbage collection to do your work for you is never the nicest way of writing code. (See the answers to open read and close a file in 1 line of code for information as to why).
Your code as under is valid:
fighter_match_data = json.load(open(file="fighter_data.txt", mode="r"))
Consider this part:
open(file="fighter_data.txt", mode="r") . #1
v/s
data_file = open(file="fighter_data.txt", mode="r") . #2
In case of #2, in case you do not explicitly close the file, the file will automatically be closed when the variable ceases to exist[In better words, no reference exists to that variable] (when you move out of the function).
In case of #1, since you never create a variable, the lifespan of that implicit variable created for opening that file ceases to exist on that line itself. And python automatically closes the file after opening it.

how do I insert data to an arbitrary location in a binary file without overwriting existing file data?

I've tried to do this using the 'r+b', 'w+b', and 'a+b' modes for open(). I'm using with seek() and write() to move to and write to an arbitrary location in the file, but all I can get it to do is either 1) write new info at the end of the file or 2) overwrite existing data in the file.
Does anyone know of some other way to do this or where I'm going wrong here?
What you're doing wrong is assuming that it can be done. :-)
You don't get to insert and shove the existing data over; it's already in that position on disk, and overwrite is all you get.
What you need to do is to mark the insert position, read the remainder of the file, write your insertion, and then write that remainder after the insertion.

Python redirect/write output to file?

I want to write the output write the output in a file, but all I got was "None" even for words with synonyms.
Note: when I am not writing in the file the output it works perfectly fine Another note: the output appears on the screen whether i am writing to a file or not, but I get "None" in the file, is theres anyway to fix it.
[im using python V2.7 Mac version]
file=open("INPUT.txt","w") #opening a file
for xxx in Diacritics:
print xxx
synsets = wn.get_synsetids_from_word(xxx) or []
for s in synsets:
file.write(str(wn._items[s].describe()))
I tried to simplify the question and rewrite your code so that its an independent test that you should be able to run and eventually modify if that was the problem with your code.
test = "Is this a real life? Is this fantasy? Caught in a test slide..."
with open('test.txt', 'w') as f:
for word in test.split():
f.write(word) # test.txt output: Isthisareallife?Isthisfantasy?Caughtinatestslide...
A side note it almost sounds like you want to append rather than truncate, but I am not sure, so take a look at this.
The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.
file.write() is going to write whatever is returned by the describe() command. Because 'None' is being written, and because output always goes to the screen, the problem is that describe is writing to the screen directly (probably with print) and returning None.
You need to use some other method besides describe, or give the correct parameters to describe to have it return the strings instead of printing them, or file a bug report. (I am not familiar with that package, so I don't know which is the correct course of action.)

delete or erase a portion of an opened file with python

is anyone could help me in finding a function that deletes just a portion from an opened file starting from its beginning. In other words, the program will open a file and read for example the first 100 bytes. Is there a built-in function on python or a way that helps me deleting just those first 100 bytes before closing the file (the file will be shifted to the right by 100 bytes). (FYI: truncate() does not help since it deletes the contents of a file starting from the current cursor position, I would like exactly the inverse-delete the content from beginning till the current cursor position and leave the rest.). Thank you
Is this something you want to do efficiently for large files, or just something you want to do in general?
It's pretty easy to do by reading in the file, and then writing it out:
import os
dat = open(filename, 'rb').read()
open(filename+'_temp', 'wb').write( dat[100:] )
os.rename(filename+'_temp',filename)
Note that this operates "safely" by first creating the new file, then moving it into place. If there is a failure anywhere, the old file will not be clobbered.

Categories

Resources