linux - how to remove unnecessary chars from date command output in python -


i new python. forgive me if it's simple. want extract date using date command in python

import subprocess  p = subprocess.popen(["date", '+%m/%d/%y'], stdout=subprocess.pipe)  output,err = p.communicate()  print (output) 

now printing

b'05/14/13\n' 

how remove unnecessary '\n' , b @ start.

thomas's answer correct, feel more explanation necessary.

i .decode('utf8') result of p.communicate() or check_output() et al. because stdout/stdin opened in binary mode, unless explicitly provide file handle, receive/send bytes, not str.

in case, suggest using check_output(['date','+%m/%d/%y']) rather creating popen object throw away :)

so, suggest rewriting to:

import subprocess result = subprocess.check_output(['date', '+%m/%d/%y']).decode('utf8').rstrip() print (result) 

on more meta level, there question of whether need use subprocess task. after all, there time.strftime() formatting dates/times. this:

import time print(time.strftime('%m/%d/%y')) 

achieves intended effect of entire program in simpler way.

also tink's comment:

 import datetime   print datetime.date.today().strftime('%m/%d/%y')  

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -