How do I store Python encoding on the command line as a file?

I just typed an example from a book in Python34 of a command line executable.

but now I want to save this python program as a file for future use. As I have never used the Command Prompt before and I have also searched the web but most of them cannot answer.

Can anyone show a solution here? Thank you.

+3


source to share


1 answer


You can save lines in ipython using % save :

Application:



% save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... Parameters:

-r: use 'raw input. The default is "processed history", so the magic is loaded into its converted version in valid Python. If this parameter is specified, the original input is used instead, which is entered as a command line.

-f: Force overwrite. If the file exists,% save will offer to overwrite if -f is not specified.

-a: add to the file, not overwrite it.

This function uses the same syntax as% history for input ranges and then stores the strings in the specified filename.

It adds the .py extension to the file if you don't, and asks for confirmation before overwriting existing files.

If the -r option is used, the default extension is .ipy.

In [1]: def foo():
   ...:     print("hello world")
   ...:     

In [2]: %save my_code 1
The following commands were written to file `my_code.py`:
def foo():
    print("hello world")


In [3]: cat my_code.py
# coding: utf-8
def foo():
    print("hello world")

      

+5


source







All Articles