如何在 powershell 中打印 .py 文件的特定行

How to print specific lines of .py file in powershell

假设我有一个 python 文件,其中包含说明

print("hello")
print("hello1")

我可以通过 >python something.py 在 powershell 中 运行 python 文件,但是我将如何执行特定行上的命令在 something.py 文件中。

基本上,我如何在 powershell 中显示 print("hello1") 的结果,它位于 something.py.

的第 2 行

将该特定行保存到一个单独的文件,然后 运行 使用 python:

(Get-Content something.py)[1] > one-line.py
python one-line.py

这会将 something.py 的第二行(基于 0 的索引)保存到一个 line.py,然后您可以 运行 将其作为一个单独的程序。

或者不用中间文件也可以:

$line = (Get-Content scratch.py)[1]; python -c $line