gpt4 book ai didi

python - 如何通过Python在Windows 10上使用Unix?

转载 作者:太空宇宙 更新时间:2023-11-03 15:13:14 25 4
gpt4 key购买 nike

我正在通过《Think Python》一书学习 Python。

我的电脑操作系统是 Windows 10。

我尝试使用 os.popen (http://www.greenteapress.com/thinkpython/html/thinkpython015.html 中的“14.8 Pipes”),但这本书提供了 Unix 上的示例。出于好奇,我想在 Python 脚本上运行 Unix。

我已经安装了 Git bash 和 Virtual box,但我不知道如何连接和使用 Python。

最佳答案

书中提供的例子是:

14.8 Pipes

Most operating systems provide a command-line interface, also known asa shell. Shells usually provide commands to navigate the file systemand launch applications. For example, in Unix you can changedirectories with cd, display the contents of a directory with ls, andlaunch a web browser by typing (for example) firefox.

Any program that you can launch from the shell can also be launchedfrom Python using a pipe. A pipe is an object that represents arunning program.

For example, the Unix command ls -l normally displays the contents ofthe current directory (in long format). You can launch ls withos.popen1:

>>> cmd = 'ls -l'

>>> fp = os.popen(cmd)

看来您在将列出 *nix 上的目录内容的 ls 命令转换为 Windows 命令时遇到了麻烦。如果你在搜索引擎中搜索“ls的windows版本是什么”,你会发现Windows通过dir提供了类似的功能。如需更多有用的转换,请查看lemonda.net上的转换表。 .

将代码更改为

>>> cmd = 'dir' 并通过调用它

>>> fp = os.popen(cmd)

应使示例能够在 Windows 上运行。

关于python - 如何通过Python在Windows 10上使用Unix?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44073785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com