简体   繁体   中英

Cannot log to a text file from Jupyter notebook

I am trying to write to a text file to log data as it is being processed. I did this in a python file and it worked fine, but when I tried from a Jupyter notebook it failed.

What worked from the python file and what I tried to use in the notebook is this:

f = open('./data/data_log/log'+ str(time) +'.txt', 'w')
print >> f, '#########################################'
print >> f, 'New log opened'
print >> f, '#########################################'

Then, in the file when a certain variable goes by I would want to write it so I can track the data as it is goes through the pipeline. In the python file I achieved that the same way, for example:

# Printing this data to the file.
print >> f, 'Steering check :', steering_check

The '>>' seems to be unsupported because it throws an error mentioning that.

jupyter notebook unsupported operand type(s) for >>:

I was unable to find through googling and searching here a way to do this.

Can anyone here point me in the right direction, even a link to documentation or something will suffice, I just cannot located the answer showing how to do this in a Jupyter notebook.

You should use: f.write('#######') .

The print chevron syntax is not supported in Jupyter notebooks.

See the difference between the two syntaxes in this question .

You will have to put spaces between arguments and line terminators yourself.

You can just use .write() which imo looks nicer

f = open('./data/data_log/log'+ str(time) +'.txt', 'w')
f.write('#########################################\n')
f.write('New log opened\n')
f.write('#########################################\n')

And

f.write('Steering check : ' + steering_check + "\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM