How to replace contents in file in python - python

I have a script in python, part of the script involves me creating a .txt file with contents i will need to use throughout the script
my_file = open('number.txt', 'w')
my_file.write(str(30))
my_file.close()
There are two things i'm stuck on, firstly, considering there will only ever be one number in the file, how would i open it and assign the contents to a variable. I know to open the file and read it would be something like this:
my_file = open('number.txt', 'r')
but then i want to assign the contents which right now is 30 to a variable called 'num' which i could use throughout the rest of my script.
Secondly, how could i replace the contents of the file. as in re-open it and replace the 30 with a different number, and would this also change automatically for the variable aswell?

Related

Load new values from another Python script

I have a device which I am controlling in a for loop. I would like to check every iteration if a .txt file has changed. If it has I want to read values and send them to the device. I am checking if file was updated like this:
os.stat("myfile.txt").stat.st_mtime
This is working fine when I manually open file, write values and save file.
I want to change values by another Python script which will be run by another process. In this other script I write values to the .txt file like this:
text_file = open("myfile.txt", 'w')
text_file.write("\n0\n0\n0")
text_file.close()
When I call open(), st_mtime changes and I load nothing because text file is empty. How to deal with this? Are there other approaches besides a text file to set new values by another Python process?
You could try an alternate way to check if the contents have changed, by checking for MD5 checksum for example.
import hashlib
..
my_hex = hashlib.md5(text_file.read()).hexdigest()
You can now monitor my_hex every iteration to check if your file contents have changed.
I used 3 files. I check if 3th files st_mtime has changed. I write new values to a second file, and than open and close 3th file. St_mtime of the 3th file changes so i load values from second file safely. :)

Strings aren't being written to new file

I have a list of JSON objects stored as a text file, one JSON object per line (total size is 30 GB), and what I'm trying to do is extract elements from those objects and store them in a new list. Here is my code to do that
print("Extracting fingerprints...")
start = time.time()
for jsonObj in open('ctl_records_sample.jsonlines'):
temp_dict = {}
temp_dict = json.loads(jsonObj)
finger = temp_dict['data']['leaf_cert']['fingerprint']
with open("fingerprints.txt", "w") as f:
f.write(finger+"\n")
finger = ""
end = time.time()
print("Fingerprint extraction finished in" + str(end-start) +"s")
Basically, I'm trying to go line-by-line of the original file and write that line's "fingerprint" to the new text file. However, after letting the code run for several seconds, I open up fingerprints.txt and see that only one fingerprint has been written to the file. Any idea what could be happening?
Your code here is the issue:
with open("fingerprints.txt", "w") as f:
f.write(finger+"\n")
The "w" part will truncate file each time it's opened.
You either want to open the file and keep it open throughout your loop, or check that the file exists and if it does open it with "a" to append.
You're opening the file in each loop iteration, in write mode as per your w parameter passed to the open function. Therefore it's being overwritten from the beginning.
You can solve it for example with two different approaches:
You can move your with statement before the for loop and everything will work, since it will be writing sequentially over the same file (using the same descriptor and pointer into the file).
Open the file in append mode each time, what will append your new written content to the end of the file. To do so, replace your w with an a.
When calling open() with the "w" mode, all the file contents will be deleted. From the Python documentation for the open() function:
'w': open for writing, truncating the file first
I think you are looking to use the "a" mode, which appends new contents to the end of the file:
'a': open for writing, appending to the end of the file if it exists
with open("fingerprints.txt", "a", newline="\n") as f:
f.write(finger)
(You can also drop the +"\n" to the f.write() call by passing the newline="\n" argument to open().)

Why a file cannot be read several times using the same handler?

I open several text file (STL) and run several operation on them using two functions previously defined. Precisely, the function "point_stl" extract the coordinates of the points contained in an STL file while the function "point_cloud" extracts the points from the STL files without repetitions.
with open(folder+"bone_set1.stl", "r") as f1, open(folder+"bone_set2.stl", "r") as f2:
var1 = point_stl(f1,f2)
var2 = point_cloud(f1,f2)
Why does it seem I can't use twice the variables f1 and f2? If I use them in the first function I don't get any results in the second and vice-versa.
Probably you are reading the files to the en inside the point_stl call.
The remedy is to to seek the files back to position 0 before caling point_cloud:
with open(folder+"bone_set1.stl", "r") as f1, open(folder+"bone_set2.stl", "r") as f2:
var1 = point_stl(f1,f2)
f1.seek(0)
f2.seek(0)
var2 = point_cloud(f1,f2)
To understand better: the most common use case of textfiles in Python is to read line after line, operating on the data on each line - that is probably what yoru code does inside those functions. The matter is that a file, once open, holds an internal "pointer" to the position up to it were read, and from which place it will resume reading on the next call. Your first function is likely reading the files to the end, and the pointers are at the end of the file. On calling the second function, tehre is nothing left to be read.
Now, the operating system file access has this "seek" function which allows one to place the file pointer at an arbitrary position - for text files, it mostly makes sense to position it either on the begining, at the end, or at a previously stored position (in another variable). By calling it with "0", and suppressing the second ("whence") parameter , both files are re-winded to the beginning.
instead of passing in a filepointer just use f.read() and pass in file contents
with open(folder+"bone_set1.stl", "r") as f1, open(folder+"bone_set2.stl", "r") as f2:
contentsf1 = f1.read()
contentsf2 = f2.read()
var1 = point_stl(contentsf1,contentsf2)
var2 = point_cloud(contentsf1,contentsf2)
Since you pass directly the file handlers, I assume that each function reads the files content.
Unfortunatly, after the first function has read the files, the read cursors are at the end of the files and the second function has nothing to read between the cursors and the end of the files.
My advice would be to:
first read the files outside the functions (before calling them)
then pass the files content to the function instead of the file handlers.
An other solution would be to set back the read cursors at the files start after the first function call.

Python opening a file and turning it into a list

I am trying to open a file and then take that file and turn it into a list I'm kinda lost as to how to get it into a list i know I can open a file with open() I don't want to use the read.line either
Input (build1,200),(build2,267) all in a txt file that needs to be opened
Output
Build1,200
Build2,200
Every time I try to add the info to a list it just adds the first one then it stops .
This will put the each line into separate sub lists in a 2d list:
sav = []
with open("filename", "r") as fileopen:
for line in fileopen:
sav.append(line.split())
I'm assuming you are using a .txt file.
This is basically going to make an sequence named 'tup'. What open() does is open up the file. The two arguments that you pass will be the 'filename' and the what you want to do with the contents of the file. Filename is going to be the entire directory of the file, i.e "C:/User...../file.txt". What 'r' signify is 'read' a file only. tuple() will create a sequence of data from your file which will be immutable (you cannot change it), but you can access the data within it.
tup=tuple(open(file,'r'))

Script to search and replace strings in a flie

I'm new to Python and am struggling to understand why this program
#!/usr/bin/env python
infile = open('/usr/src/scripts/in_file.conf')
outfile = open('/usr/src/scripts/in_file.conf', 'w')
replacements = {'abcd':'ABCD', '1234':'bob'}
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)
infile.close()
outfile.close()
results in a blank file after script execution.
The original in_file.conf is:
testfile of junk
abcd
******************
1234
*************
Correct me if i'm wrong, but it is my understanding that the script opens the in_file.conf and loads the contents into two temporary files in memory, infile & outfile. the dictionary type variable replacements acts like an array to hold the "to find" and to "replace" string.
It loops over each line then a nested loop goes down the line and loads the variables src and target with the contents of the replacement variable (like an array); then writes the line, until all the lines are written.
Am I way off in my understanding?
The in_file.conf is in the same directory as the script, could it just not finding the in_file.conf and writing a blank file?
I told you i was new to python.
Kind Regards,
Reggie.
The problem is that you're opening the same file in read mode and then in write mode (which truncates the file). You should ideally have a different file for the output, but if you need the output to be in the same file, you can delete the old file and rename the new one afterwards.
Please use different files for infile and outfile. Opening a file in write mode will delete its contents. Because your infile and outfile are the same files, your file contents is deleted and your for loop is never run

Categories

Resources