![](/img/trans.png)
[英]why am i getting "variable assigned befor global declaration on python
[英]I am getting some data using powershell command from python without having a cmd in windows! how can i assigned this output to a string variable?
我正在使用 PowerShell 命令從 python 獲取一些數據,而此命令在 Windows 中沒有 cmd!
import os
windows_info = os.system('powershell.exe get-physicaldisk')
並且它成功地工作了。 但是這個輸出給出了一個'int'輸出! 我試圖通過使用將其轉換為字符串,
windows_infoEG = str(windows_info)
但是當我試圖打印它時它不會打印任何東西(但是當我檢查它的類型時它說“str”)。 我怎樣才能把它變成一個字符串,以便將輸出寫入文本文件!
這是我的全部代碼。
import os
windows_info = os.system('powershell.exe get-physicaldisk')
print(type(windows_info))
windows_infoEG = str(windows_info)
print(type(windows_infoEG))
print(windows_infoEG)
f = open("hardDriveInfo.txt", "w")
f.write("Hard Drive Info" + windows_infoEG)
f.close()
謝謝你!
os.system
返回進程的“退出代碼”(通常為 0 表示成功,非零表示失敗)。 這就是為什么你得到一個整數。 它不會將命令的標准輸出返回給您。 為此,您需要subprocess
模塊。
windows_info = subprocess.check_output(['powershell.exe','get-physicaldisk']).decode('utf-8')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.