Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print cell output to a file on colab

In Google Colab, I want to execute several lines of python code, both print and function. The goal is to redirect the result to a file.Any tips?

like image 292
Rafael Lourenço Avatar asked Dec 21 '25 15:12

Rafael Lourenço


1 Answers

  1. In the cell do you want to print to the file write the following:

    %%capture cap
    
  2. For print into a file:

    f = open("output.txt", "w") 
    print(cap, file=f)
    f.close()
    
  • Note: With very large outputs it may not work very well. The object type of variable c is CapturedIO from IPython.utils.capture
like image 90
Rafael Lourenço Avatar answered Dec 24 '25 07:12

Rafael Lourenço



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!