0

Python - 3 string need to be printed in same line. Please help!!

Please find the script and output, help me with the simple requirement! Required output is in single line, like /var/tmp/servername012017-07-17 ##########Script############ import subprocess import os import datetime import sys cdate = datetime.date.today() from subprocess import PIPE, run def out(command): result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True) return result.stdout path = '/var/tmp/' hname = out("uname -n") print (str(path) + str(hname) + str(cdate)) ############Output############## /var/tmp/servername01 2017-07-17 ############################### Required output is in single line, like /var/tmp/servername012017-07-17 ############################### Please help!! Thanks in advance

16th Jul 2017, 5:28 PM
Raaj Tilak
Raaj Tilak - avatar
4 odpowiedzi
+ 3
I cannot import 'run' from 'subprocess', so I cannot test in real... but are you sure that vars 'path', 'hname' and 'cdate' have no break line char at end or start? You can test it by embeding them inside two visibles chars: print('<'+path+'>') # if var is str, no need of str() Or even: print('<{}>'.format(path,hname)) If so, delete it before printing using slicing: print(str(path)[0:-1]+str(hname)[0:-1]+str(cdate)[0:-1]) ... or maybe require delete two chars on windows system (\r\n) so try use of -2 instead -1 at end index ;)
16th Jul 2017, 9:39 PM
visph
visph - avatar
+ 1
It should work, by using placeholders and the .format method. print("{0} {1} {2}".format(path, hname, cdate))
16th Jul 2017, 5:46 PM
Christian Luzzetti
Christian Luzzetti - avatar
0
Thank you will check and update..
16th Jul 2017, 5:49 PM
Raaj Tilak
Raaj Tilak - avatar
0
Same output :( no changes!!
16th Jul 2017, 8:53 PM
Raaj Tilak
Raaj Tilak - avatar