[英]To read from a certain text file and compare it to a string in python
[英]python read file output from certain string value
我想從打印網卡的部分讀取文本文件,然后從那里繼續,但是我不知道如何執行此操作。
import os
def main():
print "This is a file handling system"
fileHandler()
def fileHandler():
os.system('systeminfo>file.txt')
foo = open('file.txt', 'r+')
readFile = foo.read()
x = readFile.startswith('Network Card')
print x
foo.close()
if __name__ == '__main__':
main()
您不需要先將子流程輸出保存到文件中。 您可以將其標准輸出重定向到管道:
from subprocess import check_output
after_network_card = check_output("systeminfo").partition("Network Card")[2]
after_network_card
包含systeminfo
輸出,該輸出位於第一個“網卡”之后。
如果您可以將輸出作為文件對象進行訪問,則可以從"Network Card"
行開始獲取該部分:
from itertools import dropwhile
with open('file.txt') as file:
lines = dropwhile(lambda line: "Network Card" not in line, file)
您還可以for
-loop使用顯式:
for line in file:
if "Network Card" in line:
break
# use `file` here
文件是Python中各行的迭代器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.