3

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!

Nityesh Agarwal
  • 464
  • 2
  • 5
  • 18
  • 3
    `Cntrl-C` , `Cntrl-V` ? – scharette Aug 06 '18 at 13:02
  • You mean that I select each LOC and do `Ctrl-C` and `Ctrl-V` one by one on all of them?? @scharette – Nityesh Agarwal Aug 06 '18 at 13:05
  • 2
    Well, first don't write complex code in the terminal. Now, if you have written several lines of code. Use `Cntrl-A` to get all terminal content and format it to valid python in your `.py` file. – scharette Aug 06 '18 at 13:06
  • Are you using IDLE or what Terminal are you referring to ? – HaR Aug 06 '18 at 13:07
  • Yes @HaR. I meant IDLE. Any idea how I can save all my commands on IDLE to a .py file? – Nityesh Agarwal Aug 06 '18 at 13:10
  • You can try going to File->Save and save into a .py file and then remove what you don't need. – HaR Aug 06 '18 at 13:14
  • But I have written almost 25 LOC and some of them have lots of output that I don't want to copy to my .py file. Manually removing all that output from the file does not look like the best possible way to do that. I'm hoping that there's some better way to copy all those lines. @HaR – Nityesh Agarwal Aug 06 '18 at 13:21
  • 1
    I understand ,well I suggested another interpreter in the answers, but in the mean tine if you want to save the LOC you've entered, the easiest way is to save the log to a txt or .py as suggested above, and you could look for the ">>> " which signals the start of a new terminal command and then copy-paste the good lines of codes into a new file. – HaR Aug 06 '18 at 13:31
  • Alright. Thank you @HaR :) – Nityesh Agarwal Aug 06 '18 at 13:41

4 Answers4

3

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')
skierpage
  • 2,514
  • 21
  • 19
1

You can trying using another interpreter : bpython , I belive it has what you need,check it out.

Save the code you've entered to a file.

HaR
  • 987
  • 7
  • 23
0

You seem to be affected by the misconception, that the python environment is workspace-centered (similar to what I know from Smalltalk and some LISP variants):

  • fire up with an initial workspace
  • modify by your liking
  • store the result

This is unfortunately not the case. While you can import existing files, the other option is to specify an existing file as initially to be loaded and keep the interpreter open by using the -i option.

guidot
  • 5,095
  • 2
  • 25
  • 37
0

It really depends on your terminal for the exact commands.

The general idea is to copy everything (if possible) or one page at a time from the terminal into a text editor and then clean the >>> prompts (and possibly other formatting problems) in the text editor.

But anyway, typing a lot of commands directly in the execution environment if really bad practice. At least you test a handful of lines and immediately save them in a file. IDLE is great at this game...

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252