Python Forum
Python in Linux environment on RPI
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python in Linux environment on RPI
#1
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
Reply
#2
You need to post your code and any error messages between the appropriate code tags, refer to the Help Document for instructions.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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()
Reply
#4
can you try it without any threading? i suspect threading is repeating some initialization and truncating the file.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
Thanks. Tried self.running to replace gpsp, no effect.
Attempted to take off threading by commenting out threading statements but got errors.
Reply
#7
(Jun-03-2017, 02:29 PM)kendias Wrote: but got errors.
Please, do go on.
Reply
#8
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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#9
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()
Reply
#10
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 629 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Installing python packages in a virtual environment Led_Zeppelin 1 710 Aug-15-2023, 08:18 PM
Last Post: deanhystad
  Understanding venv; How do I ensure my python script uses the environment every time? Calab 1 2,155 May-10-2023, 02:13 PM
Last Post: Calab
  Python development environment standenman 3 1,521 May-04-2023, 07:24 PM
Last Post: snippsat
  Python Scripting Environment jpotter0 1 1,618 Nov-19-2022, 03:07 PM
Last Post: snippsat
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  How do I link the virtual environment of that project to the 3.9.2 version of python? Bryant11 1 1,331 Feb-26-2022, 11:15 AM
Last Post: Larz60+
  Python syntax in Linux St0rmcr0w 2 44,674 Jul-29-2021, 01:40 PM
Last Post: snippsat
  VS Code debugger using wrong Python environment topfox 0 2,428 Jun-09-2021, 10:01 AM
Last Post: topfox
  Login to NordVPN on Linux with python script AGreenPig 2 5,906 Feb-09-2021, 10:44 AM
Last Post: AGreenPig

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020