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. :)
Related
I found a way to print the list to a txt file, and it worked for me. Except for one detail,
with open("data.txt", "w") as output:
output.write(str(list))
When I make a different data entry, new data replaces the old data I entered, I want it not to delete the old data and continue recording new data.
using "w" in the open function will rewrite the complete file so use "a" in the open function which will append the file and you will not lose the original text in the file
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?
I am a new python learner and now i have entered into file handling.
I tried solution for my problem but failed, so posting my question. before duplication please consider my question.
I tried to create a file, it worked.
writing in the file also worked.
But when i tried to read the text or values in the file, it returns empty.
I use command line terminal to work with python and running in Ubuntu OS.
The coding which I have tried is given below. The file is created in the desired location and the written text is also present.
f0=open("filehandling.txt","wb")
f0.write("my second attempt")
s=f0.read(10);
print s
I also tried with wb+, r+. But it just returns as empty
edit 1:
I have attached the coding below. I entered one by one in command line
fo = open("samp.txt", "wb")
fo.write( "Text is here\n");
fo.close()
fo = open("samp.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
fo.close()
First of all if you open with wb flag then the file will be only in writeable mode. If you want to both read and write then you need wb+ flag. If you don't want the file to be truncated each time then you need rb+.
Now files are streams with pointers pointing at a certain location inside the file. If you write
f0.write("my second attempt")
then the pointer points at the [pointer before writing] (in your case the begining of the file, i.e. 0) plus [length of written bytes] (in your case 17, which is the end of the file). In order to read whole file you have to move that pointer back to the begining and then read:
f0.seek(0)
data = f0.read()
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.
I'm trying to use a subprocess to write the output to a data file, and then parse through it in order to check for some data in it. However, when I need to do the reading through the file's lines, I always get a blank file unless I close the file and then reopen it. While it works, I just don't like having to do this and I want to know why it happens. Is it an issue with subprocess, or another intricacy of the file mode?
dumpFile=open(filename,"w+")
dump = subprocess.Popen(dumpPars,stdout=dumpFile)
dump.wait()
At this point, if I try to read the file, I get nothing. However, it works fine by doing these commands after:
dumpFile.close()
dumpFile=open(filename,"r")
The with statement automatically closes the file after the block ends:
with open(filename, "w+") as dumpFile:
dump = subprocess.Popen(dumpPars, stdout=dumpFile)
dump.wait()
with open(filename, "r") as dumpFile:
# dumpFile reading code goes here
You probably need to seek back to the beginning of the file, otherwise the file pointer will be at the end of the file when you try to read it:
dumpFile.seek(0)
However, if you don't need to actually store dumpFile, it's probably better to do something like:
dump = = subprocess.Popen(dumpPars,stdout=subprocess.PIPE)
stdoutdata,_ = dump.communicate() #now parse stdoutdata
unless your command produces large volumes of data.
If you want to read what you've already written, either close and reopen the file, or "rewind" it - seek to offset 0.
If you want to read the file while it is being written, you can do so (don't even need to write it to disk), see this other question Capture output from a program