Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save code at the python prompt in the terminal to a local file

I have just written a bunch of lines of code on the Python prompt at the terminal. Now, I want to save all those lines of code to a .py file.

I am unable to find out how to do that. The only thing that I could find on StackOverflow was this answer but it shows only how to do it in an iPython notebook. I am not using an iPython notebook. I am running the code at the command line on the terminal.

I tried to follow that answer (because just in case) and ran the %save magic command on the terminal but it gave a SyntaxError.

So, how do save it?

Thanks!

like image 695
Nityesh Agarwal Avatar asked Oct 23 '25 05:10

Nityesh Agarwal


1 Answers

See http://blog.e-shell.org/174 . As wu explains, the python prompt is using readline, and you can import a Python library to access this.

>>> import readline
>>> readline.write_history_file('/path/to/history.txt')
like image 184
skierpage Avatar answered Oct 25 '25 21:10

skierpage



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!