[英]How to print specific lines of .py file in powershell
假設我有一個帶有說明的 python 文件
print("hello")
print("hello1")
我可以通過執行 >python something.py 在 powershell 中運行 python 文件,但是我將如何執行位於 something.py 文件中特定行上的命令。
基本上,我將如何在 something.py 的第 2 行的 powershell 中顯示 print("hello1") 的結果。
將該特定行保存到單獨的文件中,然后使用 python 運行它:
(Get-Content something.py)[1] > one-line.py
python one-line.py
這會將 something.py 的第二行(基於 0 的索引)保存到 one-line.py,然后您可以將其作為單獨的程序運行。
或者您可以在沒有中間文件的情況下執行此操作:
$line = (Get-Content scratch.py)[1]; python -c $line
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.