write os.system generated message to csv-file

Asked by Peter Samulat

I like to add a row to an existing csv-file. The row should contain a timestamp (t1), text information and a numeric value (t2). t1 is right now always zero, so my first question is how to get the message "2014-01-14 18:42:07" into t1 or into the csv-row itself?

t1=os.system("echo $(date +'%Y-%m-%d %H:%M:%S')")
t2=567

with open('eggs.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow([t1, 'grp_cci','Baked Beans', t2])

Second question: this script is always creating a new csv-file "eggs-csv" with only one row. But I need to add this row to an existing file. What do i have to change?

Peter

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

at 1:
t1 = time.strftime("%Y-%m-%d %H:%M:%S")

at 2:
with open('eggs.csv', 'a') as csvfile:

a -- to open in append mode
no b (means binary) since this is a textile

Revision history for this message
Peter Samulat (peter-samulat) said :
#2

Great, thank you! It works fine.
But is there (in addition) any way to get the message generated by
os.system("echo $(date +'%Y-%m-%d %H:%M:%S')")
into a local variable?

Revision history for this message
obiwan-92 (obiwan-92) said :
#3

Hello,

Like RaiMan said,
t1 = time.strftime("%Y-%m-%d %H:%M:%S") # return the time system and save it in the local variable t1

Regards.

Revision history for this message
RaiMan (raimund-hocke) said :
#4

os.system() executes the given command, but does not return the content of stdout, but only the return code of the executed command.

So in your case the date string is printed on stdout (IDE: message area) and t1 contains 0 (since echo returned 0).

Revision history for this message
obiwan-92 (obiwan-92) said :
#5

Hello,

If you really want the result of your command try this :

import subprocess as sub
p = sub.Popen("echo $(date +'%Y-%m-%d %H:%M:%S')",stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()
print output

It should be work.

Regards

Revision history for this message
Best RaiMan (raimund-hocke) said :
#6

@obiwan-92
With subprocess.Popen() you have to either use a list of strings building the command line with the first being the program to run or one string, that is taken as the command with it's parameters (difficult if some quoting is needed.

Since he uses $() it seems to be a unix-like system (Mac or Linux). The the program to run the echo is the shell.
For these cases Popen has to be told to use the shell:

p = sub.Popen("echo $(date +'%Y-%m-%d %H:%M:%S')",shell=True,stdout=sub.PIPE,stderr=sub.PIPE)

Revision history for this message
Peter Samulat (peter-samulat) said :
#7

Thanks RaiMan, that solved my question.

Revision history for this message
obiwan-92 (obiwan-92) said :
#8

That's correct.
I'm sorry, I don't have Unix for now, so I'm testing it on Windows.

Thanks a lot Raiman.

Regards.