比较批处理和 python 命令?

发布于 2024-10-07 11:29:08 字数 664 浏览 0 评论 0原文

好的,我批量使用了这些命令,我​​想知道 python 中的命令会产生类似的影响,只是为了明确我不想只使用 os.system("command here")对于他们每个人来说。例如,在批处理中,如果您想要命令列表,您可以输入 help,但在 python 中,您可以输入 help(),然后输入 modules。我不想在 python 脚本中使用批处理,我只是想知道两种语言的相似之处。就像在英语中你说“Hello”,但在法语中你说“Bonjour”而不是混合两种语言。 (这里是我想知道的命令/函数列表:

  1. 更改当前目录
  2. 清除控制台中的屏幕
  3. 将提示更改为 >>> 以外的内容
  4. 如何进行循环函数
  5. 重定向/pipes
  6. 从脚本中启动外部程序(如记事本或画图)
  7. 如何调用或导入另一个 python 脚本
  8. 如何获得特定模块的帮助而无需输入 help()

@8: (批处理中它将是命令/?

已完全编辑

预先感谢!

Ok i have these commands used in batch and i wanted to know the commands in python that would have a similar affect, just to be clear i dont want to just use os.system("command here") for each of them. For example in batch if you wanted a list of commands you would type help but in python you would type help() and then modules... I am not trying to use batch in a python script, i just wanna know the similarities in both languages. Like in english you say " Hello" but in french you say "Bonjour" not mix the two languages. (heres the list of commands/functions id like to know:

  1. change the current directory
  2. clear the screen in the console
  3. change the prompt to something other than >>>
  4. how to make a loop function
  5. redirections/pipes
  6. start an exteral program (like notepad or paint) from within a script
  7. how to call or import another python script
  8. how to get help with a specific module without having to type help()

@8: (in batch it would be command /?)

EDITED COMPLETELY

Thanks in Adnvance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

和我恋爱吧 2024-10-14 11:29:08

您不能只是机械地将批处理脚本翻译为 Python 并希望它能起作用。这是一种不同的语言,有不同的习语和做事方式,更不用说不同的目的了。

我在下面列出了一些与您想要的功能相关的功能,但是没有什么可以替代直接学习 Python!


  1. os.chdir

  2. os.system("cls") 可能是最简单的解决方案

  3. 更改 sys.ps1sys.ps2

  4. 不,Python 中没有 goto。请改用 forwhile 循环。

  5. 没有意义,用Python的IO代替。

  6. subprocess.Popen

  7. 没有意义,请使用 importsubprocess.Popen 代替。

  8. 帮助

You can't just mechanically translate batch script to Python and hope that it works. It's a different language, with different idioms and ways of doing things, not to mention the different purpose.

I've listed some functions related to what you want below, but there's no substitute for just going and learning Python!


  1. os.chdir

  2. os.system("cls") is probably the simplest solution

  3. Change sys.ps1 and sys.ps2.

  4. Nope, there are no gotos in Python. Use for and while loops instead.

  5. Doesn't make sense, use Python's IO instead.

  6. subprocess.Popen

  7. Doesn't make sense, use import or subprocess.Popen instead.

  8. help

少年亿悲伤 2024-10-14 11:29:08

您提到的大多数内容(startcls 等)都不是“批处理命令”,它们是执行某些任务的可执行程序。当 DOS“shell”在文件中遇到它们时,它只是执行它们。从这个意义上说,“python”相当于单个可执行文件(如cls)。

现在很清楚了,cd(以及大多数其他操作系统特定的任务)是使用 操作系统模块。没有单个 Python 语句可以清除屏幕 - 这会造成浪费。可以通过分配给 sys.ps1 来更改 python 解释器的提示符。循环是使用 whilefor 完成的。重定向不会发生。但是,您可以使用 subprocess 模块来运行子命令并将其输出发送到文件或其他流。启动命令是使用 subprocess.Popen 函数完成的。要获得帮助,您可以执行 help("command") 或者如果您使用 ipython ,只需说出命令?并按回车键即可。

您确实应该阅读教程,而不是尝试映射批处理命令 到Python。

Most of the things you've mentioned (start, cls etc.) are not "batch commands", they're executable programs which perform certain tasks. The DOS "shell" simply executes these when it encounters them in a file. In this sense, "python" is the equivalent of a single executable (like cls).

Now that that's clear, cd (and most other OS specific tasks) are accomplished using the os module. There's no single Python statement to clear the screen - that would be wasteful. Changing the prompt of the python interpreter can be done by assigning to sys.ps1. Loops are done using while or for. Redirection doesn't happen. YOu can however use the subprocess module to run subcommands and send their outputs to files or other streams. Starting commands is done using the subprocess.Popen function. For getting help, you can either do help("command") or if you're using ipython, just say command? and hit enter.

You should really go through the tutorial rather than trying to map batch commands to Python.

找回味觉 2024-10-14 11:29:08

Python 不是系统 shell,Python 是一种多范式编程语言。

如果您想将 .bat 与任何内容进行比较,请将其与 sh 进行比较> 或 bash。 (您也可以在各种平台上使用它们 - 例如,用于 Windows 的 sh 位于 MinGW 包中)。

Python is not a system shell, Python is a multi-paradigm programming language.

If you want to compare .bat with anything, compare it with sh or bash. (You can have those on various platforms too - for example, sh for windows is in the MinGW package).

不即不离 2024-10-14 11:29:08

我几乎面临着和你同样的问题,丹尼尔11。作为解决方案,我正在学习 BATCH 命令及其含义。理解这些之后,我将用 Python 编写一个程序来执行相同的操作或完成相同的任务。

感谢 Adam V. 和 katrielatex 的见解和建议。

I am pretty much facing the same problem as you, daniel11. As a solution, I am learning BATCH commands and their meaning. After I understand those, I am going to write a program in Python that does the same or accomplishes the same task.

Thanks to Adam V. and katrielatex for their insight and suggestions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文