Python Forum

Full Version: Python in Linux environment on RPI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
my question here
I am trying to run a python script in Linux environment. It is Python 2.7.9 and the script is in folder /home/pi. I use the command f.write to write to the file and although it does open the file it does not write to it I then tried using the print statement but no, it does not print to the file but instead prints to the Python shell.
I am not sure where the Python shell features in this.
Any ideas?

my code here
You need to post your code and any error messages between the appropriate code tags, refer to the Help Document for instructions.
This is my script - no errors but it does not write to the file "locations.csv"
from gps import *
import time
import threading
f = open("locations.csv","w")

gpsd = None
class GpsPoller(threading.Thread):
   def __init__(self):
       threading.Thread.__init__(self)
       global gpsd
       gpsd=gps(mode=WATCH_ENABLE)
       self.current_value = None
       self.running = True
   def run(self):
       global gpsd
       while gpsp.running:
           gpsd.next()
if  __name__ == "__main__":
  
   gpsp=GpsPoller()
   
   try:
       gpsp.start()
       
       while True:
           f.write(str(gpsd.fix.longitude) + "," + str(gpsd.fix.latitude) + "\n") 
           
           time.sleep(30)
      
   except(KeyboardInterrupt,SystemExit):
       f.close()
       gpsp.running = False
       gpsp.join()
can you try it without any threading? i suspect threading is repeating some initialization and truncating the file.
I do not use classes at all for now but there is a strange reference to gpsp on line 16. I think it shouldn't be there. Try to replace it with self.running
Thanks. Tried self.running to replace gpsp, no effect.
Attempted to take off threading by commenting out threading statements but got errors.
(Jun-03-2017, 02:29 PM)kendias Wrote: [ -> ]but got errors.
Please, do go on.
Some explanation on the strange behavior of the GPS thingy here.

So, as I understand it, you need:

1) a thread that read the GPS device continuously, and fast enough to avoid buffer overflows on the other size. This thread maintains a set of "latest GPS values" somewhere.
2) another thread that runs at the application-defined pace, to get the current value and store them in a file.

Looking at the OP's code: GPSPoller() is a good candidate for 1) but it doesn't store whatever it receives from the GPS device. There is a loop in the main that could be implementing 2) but instead of getting the latest value from the GPSPoller, it tries to get the value from the GPS device directly, competing with the GPSPoller which is in a tight loop.
Thanks. I am able to print the GPS data but not able to write it to a file. The print statement prints the GPS Data into the python shell, but although I am able to get the file to open OK, the "f.write" statement does not write to it. My script is:
from gps import *
import time
import threading
f = open("locations.csv","w")
gpsd = None
class GpsPoller(threading.Thread):
   def __init__(self):
       threading.Thread.__init__(self)
       global gpsd
       gpsd=gps(mode=WATCH_ENABLE)
       self.current_value = None
       self.running = True
       #print (gpsd.fix.longitude)
       #print (gpsd.fix.latitude)
     
   def run(self):
       global gpsd
       while gpsp.running:
           gpsd.next()
           print (gpsd.fix.longitude)
           print (gpsd.fix.latitude)
        
if  __name__ == '__main__':
  
   gpsp=GpsPoller()
   try:
       gpsp.start()
      
       while True:
           f.write(str(gpsd.fix.longitude) + "," + str(gpsd.fix.latitude) + "\n") 
        
           time.sleep(30)
   except(KeyboardInterrupt,SystemExit):
       f.close()
       gpsp.running = False
       gpsp.join()
you need to put the save in the thread run method.
There is nothing that is communicating with the thread, so your write never receives any data.
And while at it, move the open there as well, and use the with open(...) as f:
so in the end run will look like (or similar to as I have not tested):
   def run(self):
       global gpsd
       while gpsp.running:
           with open(locations.csv","w") as f:
               gpsd.next()
               f.write('{}, {}\n'.format(gpsd.fix.longitude, gpsd.fix.latitude))
               # print (gpsd.fix.longitude)
               # print (gpsd.fix.latitude)
Pages: 1 2 3