Home > Blockchain >  python subprocess module changes date format
python subprocess module changes date format

Time:12-25

♠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 ?

CodePudding user response:

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'
  • Related