這裏我的命令通過任何一種python方式在windows cmd中執行。 淨使用j:\我的-PC \ d \ SharedFolder「cmd執行但python os.popen和subprocess.call和subprocess.popen和os.system不執行,爲什麼?
>>> print os.popen('net use').read()
New connections will be remembered.
Status Local Remote Network
-------------------------------------------------------------------------------
OK M: \\Purushoth-pc\d\marriagePhotosCh
Microsoft Windows Network
OK N: \\Purushoth-pc\d\Materials
Microsoft Windows Network
The command completed successfully.
>>> print os.popen('net use J: \\Purushoth-pc\d\Materials').read()
>>> subprocess.Popen('net use J: \\Purushoth-pc\d\Materials', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
('', 'System error 67 has occurred.\r\n\r\nThe network name cannot be found.\r\n\r\n')
>>> os.system('net use J: \\Purushoth-pc\d\Materials')
2
>>> subprocess.call('net use J: \\Purushoth-pc\d\Materials /persistent:yes', shell=True)
2
>>> subprocess.check_call('net use J: \\Purushoth-pc\d\Materials /persistent:yes', shell=True)
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
subprocess.check_call('net use J: \\Purushoth-pc\d\Materials /persistent:yes', shell=True)
File "C:\Python27\lib\subprocess.py", line 541, in check_call
raise CalledProcessError(retcode, cmd)
CalledProcessError: Command 'net use J: \Purushoth-pc\d\Materials /persistent:yes' returned non-zero exit status 2
知道怎樣才能蟒蛇運行這個上面的命令? 條件:不使用任何第三方模塊 - 提前強制性
請幫我弄清楚這個問題,謝謝
你運行了命令TWICE。第一個成功並創建了你的'N:'網絡驅動器。第二個FAILS,因爲'N:'已經存在。 –
你確定'N:'還不存在嗎?看起來如果它已經存在,該命令將以代碼2退出。這就是爲什麼第一次調用返回2,第二次調用拋出異常(除非它爲0)。你可以在第三個電話中看到錯誤。另外['os.popen'](https://docs.python.org/2/library/os.html#os.popen)已棄用。 – xZise
@MarcB我不認爲第一次調用成功了。 ['subprocess.call()'](https://docs.python.org/2/library/subprocess.html#subprocess.call)只會退出代碼,並且如果≠0,不會引發異常。 – xZise