在編譯器下執行五回傳
>>> import os
>>> p = os.popen("ipconfig")
>>> p.readlines()
[]
但是在cmd下面進入python 再執行就有回傳。請問怎么解決???
uj5u.com熱心網友回復:
ipconfig檔案 路徑問題uj5u.com熱心網友回復:
絕對路徑試一哈uj5u.com熱心網友回復:
我是這個路徑C:\Windows\System32\ipconfig.exe 你找一下你的uj5u.com熱心網友回復:
謝謝回復 但是這個popen是呼叫命令列的命令啊 并不是去打開檔案啊。為什么還涉及到路徑?uj5u.com熱心網友回復:
執行命令時,其實就是打開可執行檔案的。 ipconfig 命令就是運行ipconfig.exe程式檔案。只是一般情況下環境變數中配置了ipconfig的執行檔案路徑,所以在cmd執行命令時沒有使用絕對路徑去運行ipconfig執行檔案。uj5u.com熱心網友回復:
>>> p = os.popen("time")>>> p.readlines()
['\xb5\xb1\xc7\xb0\xca\xb1\xbc\xe4: 13:22:36.71\n', '\xca\xe4\xc8\xeb\xd0\xc2\xca\xb1\xbc\xe4: ']
謝謝回復 但是同樣執行 像time這樣的命令就能執行。也并沒有填寫絕對路徑啊。
uj5u.com熱心網友回復:
import os
p = os.popen("ipconfig")
lines = p.readlines()
for line in lines:
print(line)
試試這個
uj5u.com熱心網友回復:
試過了 還是空的回傳uj5u.com熱心網友回復:
os.popen(command[, mode[, bufsize]])Open a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as the corresponding argument to the built-in open() function. The exit status of the command (encoded in the format specified for wait()) is available as the return value of the close() method of the file object, except that when the exit status is zero (termination without errors), None is returned.
Availability: Unix, Windows.
Deprecated since version 2.6: This function is obsolete. Use the subprocess module. Check especially the Replacing Older Functions with the subprocess Module section.
Changed in version 2.0: This function worked unreliably under Windows in earlier versions of Python. This was due to the use of the _popen() function from the libraries provided with Windows. Newer versions of Python do not use the broken implementation from the Windows libraries.
uj5u.com熱心網友回復:
從python2.6不推薦使用這個介面了 , 你可以看哈, 你可以設定下bufsize試下,還有可以把回傳的物件p列印出來看下,是什么原因沒有內容。uj5u.com熱心網友回復:
那我用啥方法能實作我想要的功能呢?單獨列印p是現實的這樣,但是里面是空的啊
>>> p = os.popen("ipconfig")
>>> print p
<open file 'ipconfig', mode 'r' at 0x036C87B0>
uj5u.com熱心網友回復:
我現在懷疑是不是我的win10 64系統 對python支持的不好啊uj5u.com熱心網友回復:
謝謝朋友 解決了!是鬼變數的問題。要在用戶變數哪里把cmd的路徑加上。>>> p = os.popen("ipconfig")
>>> p.readlines()
['\n', 'Windows IP \xc5\xe4\xd6\xc3\n', '\n', '\n', '\xd2\xd4\xcc\xab\xcd\xf8\xca\xca\xc5\xe4\xc6\xf7 \xd2\xd4\xcc\xab\xcd\xf8:\n', '\n', ' \xc1\xac\xbd\xd3\xcc\xd8\xb6\xa8\xb5\xc4 DNS \xba\xf3\xd7\xba . . . . . . . : \n', ' \xb1\xbe\xb5\xd8\xc1\xb4\xbd\xd3 IPv6 \xb5\xd8\xd6\xb7. . . . . . . . : fe80::9d67:8487:6d93:f302%14\n', ' IPv4 \xb5\xd8\xd6\xb7 . . . . . . . . . . . . : 172.18.116.78\n', ' \xd7\xd3\xcd\xf8\xd1\xda\xc2\xeb . . . . . . . . . . . . : 255.255.255.0\n', ' \xc4\xac\xc8\xcf\xcd\xf8\xb9\xd8. . . . . . . . . . . . . : 172.18.116.254\n', '\n', '\xd2\xd4\xcc\xab\xcd\xf8\xca\xca\xc5\xe4\xc6\xf7 \xd2\xd4\xcc\xab\xcd\xf8 2:\n', '\n', ' \xc1\xac\xbd\xd3\xcc\xd8\xb6\xa8\xb5\xc4 DNS \xba\xf3\xd7\xba . . . . . . . : \n', ' \xb1\xbe\xb5\xd8\xc1\xb4\xbd\xd3 IPv6 \xb5\xd8\xd6\xb7. . . . . . . . : fe80::39f5:3175:aaff:d7e0%13\n', ' IPv4 \xb5\xd8\xd6\xb7 . . . . . . . . . . . . : 192.168.213.1\n', ' \xd7\xd3\xcd\xf8\xd1\xda\xc2\xeb . . . . . . . . . . . . : 255.255.255.0\n', ' \xc4\xac\xc8\xcf\xcd\xf8\xb9\xd8. . . . . . . . . . . . . : \n', '\n', '\xd2\xd4\xcc\xab\xcd\xf8\xca\xca\xc5\xe4\xc6\xf7 \xd2\xd4\xcc\xab\xcd\xf8 3:\n', '\n', ' \xc1\xac\xbd\xd3\xcc\xd8\xb6\xa8\xb5\xc4 DNS \xba\xf3\xd7\xba . . . . . . . : \n', ' \xb1\xbe\xb5\xd8\xc1\xb4\xbd\xd3 IPv6 \xb5\xd8\xd6\xb7. . . . . . . . : fe80::c19f:b619:d9e6:6731%8\n', ' IPv4 \xb5\xd8\xd6\xb7 . . . . . . . . . . . . : 192.168.182.1\n', ' \xd7\xd3\xcd\xf8\xd1\xda\xc2\xeb . . . . . . . . . . . . : 255.255.255.0\n', ' \xc4\xac\xc8\xcf\xcd\xf8\xb9\xd8. . . . . . . . . . . . . : \n']
uj5u.com熱心網友回復:
謝謝朋友 解決了!是鬼變數的問題。要在用戶變數哪里把cmd的路徑加上。你這是怎么解決的,求幫助
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/89457.html
上一篇:求助大神們,想用Python寫一個處理excel的程式
下一篇:救!
