如何在Python3交互式控制台中隐藏 “>>> ” 提示?是否有一个标志用于此目的?

huangapple go评论52阅读模式
英文:

How to hide >>> prompt in python3 interactive console? Is there a flag for this?

问题

为了复制和粘贴的目的,我希望在交互式 shell 控制台中隐藏 >>>... Python 提示。我已成功通过以下命令实现这一效果:import sys,然后 sys.ps1 = ""来源)。然而,这只隐藏了 >>>。此外,一个标志会简化工作,由于其他编程语言也有这种类型的标志,我想知道是否在重复造轮子。

在使用 Db2 进行数据库编程时,例如,有一个 +p 标志。它的描述是负面的,它执行与 -p 相反的操作:

> -p 选项告诉命令行处理器在用户处于交互模式时显示命令行处理器提示符。

有什么捷径吗?

英文:

For copy and paste purposes, I wish to hide the >>> and ... Python prompts whenever I am in the interactive shell console. I have achieved this effect successfully with the command import sys, then sys.ps1 = "" (source). However that only hides >>>. Furthermore, a flag would simplify the work, and since other languages have this type of flag, I wonder if I'm not reinventing the wheel.

In database programming with Db2, for example, there is a +p flag. As it is described negatively, it does the opposite of -p:

> The -p option tells the command line processor to display the command line processor prompt when the user is in interactive mode.

Any shortcuts?

答案1

得分: 1

python -i scriptname.py 运行 scriptname.py,然后将您进入交互式 shell。如果该脚本清除了 sys.ps1sys.ps2,那么交互式 shell 将不会打印提示符。

更好的方法是,您可以使用 shell 特性 process substitution 来创建一个临时文件名(类似 /dev/fd/10,根据您的操作系统而定),当读取时,其中包含您想要的脚本:

pnp() { python -i <(printf '%s\n' 'import sys' 'sys.ps1=""' 'sys.ps2=""') "$@"; }

...如果将其放置在您的 .bashrc 中,将定义一个名为 pnp 的命令("python, no prompt"),执行上述操作。

英文:

python -i scriptname.py runs scriptname.py, then drops you to an interactive shell. If that script clears sys.ps1 and sys.ps2, then the interactive shell will be one that doesn't print prompts.

Even better, you can use the shell feature process substitution to create a temporary filename (something like /dev/fd/10, depending on your OS) that, when read, contains exactly the script you want:

pnp() { python -i <(printf '%s\n' 'import sys' 'sys.ps1=""' 'sys.ps2=""') "$@"; }

...if placed in your .bashrc, will define a command pnp ("python, no prompt") that does the above.

huangapple
  • 本文由 发表于 2023年2月20日 00:47:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定