Python Forum
Script won't continue after command line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script won't continue after command line
#1
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?
Reply
#2
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.
Reply
#3
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.
Reply
#4
do you have sudo access on the computer this is running on? Is that the problem?
Reply
#5
(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.
Reply
#6
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.
Reply
#7
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.
Reply
#8
(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}'])
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 635 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Command line argument issue space issue mg24 5 1,280 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 1,242 Sep-27-2022, 05:58 PM
Last Post: snippsat
  Help, a script line is skipped kucingkembar 5 1,391 Dec-29-2021, 07:34 PM
Last Post: deanhystad
  Accessing varying command line arguements Rakshan 3 2,010 Jul-28-2021, 03:18 PM
Last Post: snippsat
  Run CMD Command In Python Script shantanu97 2 6,465 May-13-2021, 08:08 AM
Last Post: Gribouillis
  How to input & output parameters from command line argument shantanu97 1 2,508 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  How to skip to a different line while importing a different script kat_gamer 2 2,209 Feb-03-2021, 04:10 AM
Last Post: deanhystad
  read the first line in my script chubbychub 1 1,698 Nov-09-2020, 03:18 AM
Last Post: Larz60+
  Passing List of Objects in Command Line Python usman 7 3,092 Sep-27-2020, 03:45 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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