Marco Nappi
Marco Nappi

Reputation: 39

python subprocess module changes date format

♠im using this command to get the timestamp ( in epoch time) of a request.

I was using the bash command date and it worked for me but using this python cmd i obtain the date in another format.

bash

date +%s%3N

bash output

1640426436567

python

import subprocess
start = subprocess.Popen(["date", "+%s%3N"], stdout=subprocess.PIPE, shell=True).communicate()[0]
print(str(start))

python output

b'Sat Dec 25 05:37:04 AM EST 2021\n

any idea how to solve this issue ?

Upvotes: 0

Views: 462

Answers (1)

ufopilot
ufopilot

Reputation: 3975

Try this:

import subprocess
start1 = subprocess.Popen("date +%s%3N", stdout=subprocess.PIPE, shell=True).communicate()[0]
start2 = subprocess.Popen(["date", '+%s%3N'], stdout=subprocess.PIPE).communicate()[0]
print("shell=True", start1)
print("shell=False", start2)

Output

shell=True b'1640435179481\n'
shell=False b'1640435179488\n'

To string -> (decode and strip)

start1 = subprocess.Popen("date +%s%3N", stdout=subprocess.PIPE, shell=True).communicate()[0].decode("utf-8").strip()

Upvotes: 1

Related Questions