How to save Python coding in Command Prompt as a file?(如何将命令提示符中的 Python 编码保存为文件?)
问题描述
我刚刚在 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 编码保存为文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将命令提示符中的 Python 编码保存为文件?


基础教程推荐
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 包装空间模型 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01