Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting a file in python

I'm trying to over write a file in python so it only keeps the most up to date information read from a serial port. I've tried several different methods and read quite a few different posts but the file keeps writing the information over and over with out overwriting the previous entry.

 import serial

 ser=serial.Serial('/dev/ttyUSB0',57600)

 target=open( 'wxdata' , 'w+' )

 with ser as port, target as outf:
      while 1:
           target.truncate()
           outf.write(ser.read))
           outf.flush()

I have a weather station sending data wirelessly to a raspberry pi, I just want the file to keep one line of current data received. right now it just keeps looping and adding over and over. Any help would be greatly appreciated..

like image 762
Joshua Foster Avatar asked Nov 21 '14 22:11

Joshua Foster


People also ask

Can I overwrite a file in Python?

01) Using write only ('w') File Access Mode (Remember to specify the file access mode in the open() function.) Python includes the write function for writing to a file. If the file contains any content, it will be completely overwritten by anything you write on the opened file.

How do I overwrite an existing file?

Save As -> Replace File If you are accustomed to selecting "File -> Save As" when saving documents, you can also overwrite the file with your changes this way. Select "Replace File. This is the same behavior as File Save." The original file will be overwritten.

How do you replace a file in Python?

replace() method in Python is used to rename the file or directory. If destination is a directory, OSError will be raised. If the destination exists and is a file, it will be replaced without error if the action performing user has permission.

How do you overwrite a value in Python?

We can also use a while loop to replace values in the list. While loop does the same work as for loop. In the while loop first, we define a variable with value 0 and iterate over the list. If value matches to value that we want to replace then we replace it with the new value.


2 Answers

I would change your code to look like:

from serial import Serial

with Serial('/dev/ttyUSB0',57600) as port:
    while True:
        with open('wxdata', 'w') as file:
            file.write(port.read())

This will make sure it gets truncated, flushed, etc. Why do work you don't have to? :)

like image 74
Travis Griggs Avatar answered Sep 28 '22 16:09

Travis Griggs


By default, truncate() only truncates the file to the current position. Which, with your loop, is only at 0 the first time through. Change your loop to:

while 1:
    outf.seek(0)
    outf.truncate()
    outf.write(ser.read())
    outf.flush()

Note that truncate() does accept an optional size argument, which you could pass 0 for, but you'd still need to seek back to the beginning before writing the next part anyway.

like image 29
Cameron Avatar answered Sep 28 '22 16:09

Cameron