Python - Reading from a text file that is being written in Windows - python

I am using Windows 7, Python 2.7. I am trying to write to a text file with one file ID in one program that continues writing new data/numbers for several minutes.
In a separate program, after the writing has begun, I am trying to read from the file that is being written in order to update and plot the data in the file.
While the first program is writing the data, I am unable to read the data until it finishes. Here is some example code to illustrate my point:
Program 1:
import time
fid = open("test1.txt", "w+")
for i in range(0, 5):
fid.write(str(i) + "\n")
print(i)
time.sleep(5)
fid.close()
Program 2:
fid = open("test1.txt", "r+")
dataList = fid.read().splitlines()
print(dataList)
fid.close()
Executing Program 2 while Program 1 is running does not allow me to see any changes until Program 1 is completed.
Is there a way to fix this issue? I need to keep the reading and writing in two separate programs.

This might be caused by buffering in program 1. You can try flushing the output in program 1 after each write:
fid.write(str(i) + "\n")
fid.flush()
Another thing you can try is to run the Python interpreter in unbuffered mode for program 1. Use the python -u option.
Also, do you need to open the file for update (mode r+) in program 2? If you just want to read it, mode r is sufficient, or you can omit the mode when calling open().

Related

How to update the file size on desk while python file writing program runs

I have a python program that performs a socket connection, get some data and writes them to a file. The data is quite large. I need to make sure that the writing is in a good progress while the program is running by clicking of the file properties to see the size increases as the program runs for more time.
To simplify, the main logic of the issue appears in this simple example:
myfile = open ("test.txt","w")
x = 0
while x < 1000000:
myfile.write("line:"+str(x))
print("line",str(x))
x+=1
This program writes 1 million line. During the writing, I check the file size and it is always 0 KB. Only when the program finishes, I can see the file size. This is inconvenient for me.
1) What is the reason for that? I recall that I had run programs but I was able to see file size progress while the program runs?
I am not sure if it was python program but I used to do this usually to monitor progress.
2) How can I reflect the file writing progress so when I click on the file properties while the python program runs, I can see the file size increases.
UPDATE:
Unfortunately none of the solutions seem to be practical. First, I do fresh writing with "w" in purpose. The program should write new fresh data every run. I do not want to append. Second, the program should write the data at onece in the loop. At the end, I should close the file (which I missed).
What I need is something like synchronization or flushing. I do not thing it is practical to close the file for a loop that take 100 thousands iterations.
You should close every file you open, but you can use a context manager to avoid calling close. In addition, you are using the 'w' mode which will overwrite the contents of your file every time you open it, so you have to change to 'a' (append mode), in order to see the file increasing in size.
x = 0
while x < 1000000:
with open ("test.txt", "a") as myfile:
myfile = open ("test.txt","a") #opens file to append
myfile.write("line:"+str(x)) # appends to file
print("line", x) # no need to call str
x += 1
This will take more time but will achieve your goal. The important part is closing the file each time.
x = 0
while x < 1000000:
myfile = open ("test.txt","a") #opens file to append
myfile.write("line:"+str(x)) # appends to file
myfile.close() # closes file(this will update the file size each time)
print("line",str(x))
x+=1
You need to close the file every time you add new data.

PyCharm outputs no text while reading file

I am new to Python and I have programmed a very basic program which opens a pre-made file reads it, then closes it, through normal Python syntax:
f = open("file", "r+")
f.read()
f.close()
However, once run, this program produces no output.
Is there a problem with my syntax, or is there an error in my PyCharm installation?
This is supposed to happen. f.read() does not print things to the screen. It returns them, just like open() returns f. If you want to print things, you need to call the print() function (under Python 3) or use the print statement (under Python 2).

Save ongoing measurement into csv file

I would like to store data into csv file. But the data are incrementing with time. I wrote a simple example to show the problem :
import csv
import time
i = 0
with open('testfile.csv','wb') as csvfile:
writer = csv.writer(csvfile,delimiter=';',quoting=csv.QUOTE_NONE)
while True:
i = i+1
print i
writer.writerow([i])
time.sleep(2)
When the while loop is running the csv file is not written. But when I stop the program then the data are stored in the csv file.
Is there a possibility to keep the program running and 'force' the writing into the csv file?
writing in python is buffered. you can force to write the output (flush the buffer) with:
csvfile.flush()
in your code i suggest you add this line right after writer.writerow([i]).
you could also pass a buffering argument to the open() function - but i suggest you do not do that; switching buffering off comes with a performance penalty.

Python: how to constantly update text lines

i have a list of 20 lines, either a 0 or a 2. right now i have it rewriting the 20 lines of the text files based off ping results. i'm having a seperate program read that 20 lines, but it generates errors when there is not 20 lines (as the text file is being written). How can i edit each individual text line without rewriting the document?
ping ip
if ping == 0
f= open("status", 'ab')
f.write("0\n")
f.close
thats one condition on how it writes. i do wipe the document before this executes.
If I understand the use of constantly in the title correctly, you're trying to pass real time data here... Programs should not communicate in real time through files. That's not stable as well as awfully slow. if that's not the case you may want to rewrite the file opening it with w (write) instead of a (append).
if ping == 0
with open("status", 'wb') as f:
# write all 20 lines
Read more about modes.
note: to actually close a file you should call file.close by using f.close() and not f.close. If you're using with as a context manager like I suggest, the file is closed once the context is over (indentation returns to with level).

Prints on stdout but unable to write to file in Python

I'm really frustrated with this strange behaviour of Python all of a sudden. I've been writing to files all sorts of data but since today morning it just doesn't seem to work. I've referred to all these before posting:
How to redirect 'print' output to a file using python?
Failed to write to file but generates no Error
Unable to write data into a file using python
Unable to write list of elements to a file using python
Have tried all following commands but it just doesn't write anything to the delete.txt file. What's happening?
fl=open('delete.txt','w')
fl.write(msg) <--Doesnt work,tried this also
fl.write('%s' %msg) <--Doesnt work,tried this also
fl.write("at least write this") <-- Doesnt work,tried this also
print (msg) <- WORKS
Code:
for i in hd_com.comment_message[1:500]:
fl=open('delete.txt','wb')
try:
if len(i)>40:
mes=processComUni(i)
proc=nltk.tokenize.word_tokenize(mes)
#print proc
pos=nltk.pos_tag(proc)
for i in pos:
if ((i[1]=="NN") or (i[1]=="NNP") or (i[1]=="NNS")) and len(i[0])>2:
#print i[0],i[1]
for j in home_depo_inv:
if i[0] in j.split() and (i[0]!='depot' and i[0]!='home' and i[0]!='store' and i[0]!='por' and i[0]!='get' and i[0]!='house' and i[0]!='find' and i[0]!='part' and i[0]!='son' and i[0]!='put' and i[0]!='lot' and i[0]!='christmas' and i[0]!='post'):
a=re.findall(i[0],j)
fl.write(str(i))<--Doesnt work,tried this also
fl.write(str(mes))<--Doesnt work,tried this also
fl.write("\n")<--Doesnt work,tried this also
fl.write("hello")<--Doesnt work,tried this also
fl.flush()
break
except:
continue
fl.close()
More code:
type(mes) = str
mes="omg would love front yard"
Your snippet's indentation is totally messed up, but anyway: your code starts with:
for i in hd_com.comment_message[1:500]:
fl=open('delete.txt','wb')
which means you reopen the file for writing on each iteration, erasing whatever might have been written by the previous iteration.
You need to flush the output stream explicitly when writing to a file handle like that.
f = open("test.txt", "w")
f.write("this is a test\n")
# no text in the output file at this point
f.flush()
# buffers are flushed to the file
f.write("this, too, is a test\n")
# again, line doesn't show in the file
f.close()
# closing the file flushes the buffers, text appears in file
From the documentation of file.write:
Note that due to buffering, flush() or close() may be needed before the file on disk reflects the data written.

Categories

Resources