bmattila55
Posts: 25
Joined: Wed Jun 06, 2018 12:54 am

Slight Issue with Python Script

Mon Sep 24, 2018 5:32 am

Hi,

I am having some issues with displaying the time using a python script. This is my code,

Code: Select all

import subprocess

time = subprocess.call(["date",  "+%H.%M"])

print(time)
When I run this script the output is this

Code: Select all

15.00
0
Any ideas to why the 0 is appearing beneath it. I need to get the value of the time and manipulate it but I can't with the 0 below.

Thanks, Ben

(I know I could just use a bash snippet but this is a small test from a bigger python script.)

User avatar
bensimmo
Posts: 3219
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Slight Issue with Python Script

Mon Sep 24, 2018 7:38 am

It may be easier to use the built in time or datetime modules?
Of course there is probably a reason you're using a system call.

DirkS
Posts: 9040
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Slight Issue with Python Script

Mon Sep 24, 2018 7:40 am

The time you see is actually the output of the 'date' command, not the returned value. (you can verify that by commenting out the print command)

Code: Select all

time = subprocess.call(["date",  "+%H.%M"])
This runs the command and then assigns the return code of the command to 'time'
The value 0 indicates that it was successful

You could use something like

Code: Select all

import subprocess

p = subprocess.Popen(["date", "+%H.%M"], stdout=subprocess.PIPE)
time, _ = p.communicate()
print(time)
This runs the date command, redirects the output and 'catches' the output with p.communicate.

User avatar
bensimmo
Posts: 3219
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: Slight Issue with Python Script

Mon Sep 24, 2018 7:45 am

That 0 is coming from your print statement.
(Comment it out and you should see that, I'm testing on Android Pydroid3)

A Python3 way of doing it, probably many others.

Code: Select all

from time import gmtime, strftime,localtime
thetime2 = strftime("%H:%M", gmtime())
thetime3 = strftime("%H:%M", localtime())
print(thetime2)
print(thetime3)

bmattila55
Posts: 25
Joined: Wed Jun 06, 2018 12:54 am

Re: Slight Issue with Python Script

Mon Sep 24, 2018 8:48 am

bensimmo wrote:
Mon Sep 24, 2018 7:38 am
It may be easier to use the built in time or datetime modules?
Of course there is probably a reason you're using a system call.
To be honest I didn't even think of that. Would you be able to give me and example?

bmattila55
Posts: 25
Joined: Wed Jun 06, 2018 12:54 am

Re: Slight Issue with Python Script

Mon Sep 24, 2018 8:52 am

DirkS wrote:
Mon Sep 24, 2018 7:40 am
You could use something like

Code: Select all

import subprocess

p = subprocess.Popen(["date", "+%H.%M"], stdout=subprocess.PIPE)
time, _ = p.communicate()
print(time)
This runs the date command, redirects the output and 'catches' the output with p.communicate.
Thanks so much, that works perfectly :)

DirkS
Posts: 9040
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Slight Issue with Python Script

Mon Sep 24, 2018 9:28 am

bmattila55 wrote:
Mon Sep 24, 2018 8:48 am
bensimmo wrote:
Mon Sep 24, 2018 7:38 am
It may be easier to use the built in time or datetime modules?
Of course there is probably a reason you're using a system call.
To be honest I didn't even think of that. Would you be able to give me and example?
The post above yours shows one of the many ways to do that: https://www.raspberrypi.org/forums/view ... 2#p1369232

bmattila55
Posts: 25
Joined: Wed Jun 06, 2018 12:54 am

Re: Slight Issue with Python Script

Mon Sep 24, 2018 9:47 am

DirkS wrote:
bmattila55 wrote:
Mon Sep 24, 2018 8:48 am
bensimmo wrote:
Mon Sep 24, 2018 7:38 am
It may be easier to use the built in time or datetime modules?
Of course there is probably a reason you're using a system call.
To be honest I didn't even think of that. Would you be able to give me and example?
The post above yours shows one of the many ways to do that: https://www.raspberrypi.org/forums/view ... 2#p1369232
Thanks

bmattila55
Posts: 25
Joined: Wed Jun 06, 2018 12:54 am

Re: Slight Issue with Python Script

Mon Sep 24, 2018 9:50 am

DirkS wrote:
Mon Sep 24, 2018 7:40 am
The time you see is actually the output of the 'date' command, not the returned value. (you can verify that by commenting out the print command)

Code: Select all

time = subprocess.call(["date",  "+%H.%M"])
This runs the command and then assigns the return code of the command to 'time'
The value 0 indicates that it was successful

You could use something like

Code: Select all

import subprocess

p = subprocess.Popen(["date", "+%H.%M"], stdout=subprocess.PIPE)
time, _ = p.communicate()
print(time)
This runs the date command, redirects the output and 'catches' the output with p.communicate.
This is the whole code.

Code: Select all

import subprocess

a = subprocess.Popen(["date", "+%H.%M"], stdout=subprocess.PIPE)
time, _= a.communicate()

b = subprocess.Popen(["date", "+%H:%M:%S"], stdout=subprocess.PIPE)
time2, _= b.communicate()

if time >= 00.01:
        time_of_day = "morning"

print("Hi, it is currently " + str(time2) + ". Have a good " + str(time_of_day) + ".")
I haven't put the other times of day in yet that was just a test. The only issue with this is this is the output.

Code: Select all

Hi, it is currently 19:19:39
. Have a good morning.
Any ideas on why it prints on two lines?

So sorry for all the questions, I feel like I should know this but I can't think of a solution for the life of me.

Thanks

B.Goode
Posts: 6445
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Slight Issue with Python Script

Mon Sep 24, 2018 10:05 am

Any ideas on why it prints on two lines?
Almost certainly because the string created by str(time2) has a newline character at the end of it?

And that in turn may be because that is what b.communicate() gives you?


Note the question marks in my reply. These are just possible reasons based on glancing at your code.

DirkS
Posts: 9040
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Slight Issue with Python Script

Mon Sep 24, 2018 10:07 am

bmattila55 wrote:
Mon Sep 24, 2018 9:50 am
Any ideas on why it prints on two lines?
Yes. The text that comes back from the date command ends with a newline, so you will see that in your output.
Use something like this to strip last character (the newline char) off the text

Code: Select all

time, _= a.communicate()
time = time[:-1]

bmattila55
Posts: 25
Joined: Wed Jun 06, 2018 12:54 am

Re: Slight Issue with Python Script

Mon Sep 24, 2018 10:09 am

DirkS wrote:
Mon Sep 24, 2018 10:07 am
bmattila55 wrote:
Mon Sep 24, 2018 9:50 am
Any ideas on why it prints on two lines?
Yes. The text that comes back from the date command ends with a newline, so you will see that in your output.
Use something like this to strip last character (the newline char) off the text

Code: Select all

time, _= a.communicate()
time = time[:-1]
That is perfect, thanks so much for that. I should have thought of that.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 11 guests