问题描述
我刚刚在 Python34 的书中键入了一个正在运行的命令提示符的示例.
I just typed an example from a book in Python34 of a running Command Prompt.
但现在我想将这个 python 程序保存为文件以备将来使用.由于我之前从未使用过命令提示符,我也在网上搜索过,但大多数都无法回答.
but now I want to save this python program as file for future uses. Since I have never used Command Prompt before and I also searched online but most of them cannot answer.
谁能在这里展示解决方案?谢谢.
Can anyone show the solution here? Thanks.
推荐答案
您可以使用 %save:
用法:
%save [选项] 文件名 n1-n2 n3-n4 ... n5 .. n6 ...选项:
%save [options] filename n1-n2 n3-n4 ... n5 .. n6 ... Options:
-r:使用原始"输入.默认情况下,使用已处理"历史记录,以便将魔法在其转换后的版本中加载到有效的 Python 中.如果给出此选项,则使用作为命令行输入的原始输入.
-r: use ‘raw’ input. By default, the ‘processed’ history is used, so that magics are loaded in their transformed version to valid Python. If this option is given, the raw input as typed as the command line is used instead.
-f:强制覆盖.如果文件存在,%save 将提示覆盖,除非给出 -f.
-f: force overwrite. If file exists, %save will prompt for overwrite unless -f is given.
-a:附加到文件而不是覆盖它.
-a: append to the file instead of overwriting it.
此函数对输入范围使用与 %history 相同的语法,然后将行保存到您指定的文件名.
This function uses the same syntax as %history for input ranges, then saves the lines to the filename you specify.
如果您自己不这样做,它会在文件中添加一个.py"扩展名,并在覆盖现有文件之前要求确认.
It adds a ‘.py’ extension to the file if you don’t do so yourself, and it asks for confirmation before overwriting existing files.
如果使用 -r 选项,默认扩展名是 .ipy.
If -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")
这篇关于如何将命令提示符中的 Python 编码保存为文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)