Python Forum

Full Version: Script won't continue after command line
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am very new to Pi, Python, and this kind of programming, as a whole.

I am building a data logger. Once completed, it will be deployed in areas with no internet connectivity. So I chose to create the build based on Widgetlords Real Time Clock Interface, Analog Input Interface and Digital Input Interface. There is an issue with the real time clock (I am working with VP Process to resolve it), therefore; I am having to enter the date and time manually. Once deployed, I want to ensure that the time and date are set before starting to collect data, so I added an input at the beginning of the script to ask for the date and time, and then set the time based on that.

#Set System Time and Date
Real_Date = input('Please enter date (ex. 10/15/2018)')
Real_Time = input('Please enter time (ex. 09:57:00)')

os.system("sudo date -s '%s %s'" % (Real_Date, Real_Time))
This portion of the script works properly. When I execute the script, it asks for the date. Once entered, it asks for the time. Once entered, you can see the time and date adjust to what was input.

However, after executing this part of the script, it does not move on to the rest of the code. It just stops there.

Here is a little more of the code so you can see the context.

import os
from time import sleep
import time
from widgetlords.pi_spi_din import *
from widgetlords import *

#Set System Time and Date
Real_Date = input('Please enter date (ex. 10/15/2018)')
Real_Time = input('Please enter time (ex. 09:57:00)')

os.system("sudo date -s '%s %s'" % (Real_Date, Real_Time))

init()
inputs = Mod8AI(ChipEnable.CE1)
inputs = Mod8DI(ChipEnable.CE2)

while True:

    #Time - ts = Timestamp, rt = Real Time
    ts = time.time()
    rt = time.ctime(ts)

    #Data Logging Files - Amp_File (Current Transducer Log), Pressure_File     (Pressure Tranducer Log)
    Amp_File = open("/home/pi/Documents/Amp_File.txt", "a")
    Pressure_File = open("/home/pi/Documents/Pressure_File.txt", "a")
What am I missing that is not allowing the code to continue to execute after the command line sequence has completed?
what does the
init()
call do? maybe it isn't returning from that, as when I ran the code snippet it worked.

Add
print "date and time set"
after the os.system call and before init. see if it prints it out.
Marienbad....thanks for the reply. I added the line:

print('Real Date is %s \n Real Time is %s' % (Real_Date, Real_Time))
It did not print anything. Still locks after entering time.

Not sure what the init() code does. That was part of the code provided by VP Process for the Widgetlords interface boards.

Actually, the test code I provide in the last response is incorrect and gives an error. However, I did not see the error because it never got done to it. I got the error after I commented out the four lines of code in question.
do you have sudo access on the computer this is running on? Is that the problem?
(Oct-15-2018, 04:35 PM)marienbad Wrote: [ -> ]do you have sudo access on the computer this is running on? Is that the problem?

I assume I do. When I run

sudo date -s '2018-10-15 12:41:00'

at the command prompt, it works. When I run it as part of the script, it works.....it changes the date and time to what is entered. It just does not allow the script to go beyond it.
when I ran it I changed input to raw_input. Try this, maybe there is an issue there. Check which version of python you are running as raw_input is in python 2.x and is now input in python3.
I'm not certain I know what raw_input is. I tried using it in place of input:

Real_Date = raw_input('Please enter date (ex. 10/15/2018)')
When I did this, I got an error:

NameError: name 'raw_input' is not defined

If this is not what you meant, then I don't understand.

Okay. I have been writing and testing the code using Thonny. I opened it and ran it using Python 3 (IDLE) and it runs perfectly. So evidently the code is okay, it's the interpreter that I am using to write and test the code.
(Oct-15-2018, 05:10 PM)skip671 Wrote: [ -> ]NameError: name 'raw_input' is not defined
Then you use Python 3,check version from command line python -V and pip -V.
Ideally you should use 3.6 or 3.7

os.system() should not be used anymore,subprocess replace it.
'%s %s'" % is the oldest string formatting in Python,so don't use it.
f-string is what should be used now,or format() for 3.5 -->.
>>> real_date = '2018-01-31'
>>> real_time = '20:45:00'
>>> print(f"sudo date -s '{real_date} {real_time}'")
sudo date -s '2018-01-31 20:45:00'
With subprocess:
import subprocess

real_date = '2017-12-31'
real_time = '20:45:00'
subprocess.run(f"sudo date -s '{real_date} {real_time}'", shell=True)
It's also safer to use pass in list,then shell is shell=False.
import subprocess

real_date = '2017-12-31'
real_time = '20:45:00'
subprocess.run(['sudo', 'date', '--set', f'{real_date} {real_time}'])