简体   繁体   中英

How to print specific lines of .py file in powershell

lets say I have a python file with the instructions

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

I can run the python file in powershell by doing >python something.py, but how would I execute a command that is on a specific line that is in the something.py file.

Basically, how would I just show the result of print("hello1") in powershell, which is on line 2 of something.py.

Save that specific line to a separate file and then run it using python:

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

This will save the second line (0 based indexing) of something.py to one-line.py and then you can run that as a separate program.

Or you can do it without an intermediate file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM