netcfg可以在cmd中运行,但在python 的os.system函数中执行却找不到该应用程序

来源:互联网 发布:威锋论坛mac讨论区 编辑:程序博客网 时间:2024/04/29 19:25

(1)netcfg可以在cmd中运行,但在python 的os.system函数中执行却找不到该应用程序

Python代码:

import osif __name__=="__main__":    print("start...")    os.system("netcfg")    print("finished!!!")

运行Python代码效果:


在cmd中直接运行netcfg效果:


可以看到在cmd中并没有报找不到netcfg的错误,而且在C:/Windows/System32中也确实有netcfg.exe这个文件。

(2)将 C:/Windows/System32 中的netcfg.exe文件复制一份到 C:/Windows/SysWOW64 文件夹下

再运行上面的Python代码,效果如下:


可以看到,(1)中的问题被解决了。。。。。。。。。。但,不知为何。

更正:

后续测试发现,(2)中的解决方案仅仅是程序运行没有报错,其实是netcfg根本就没有正常运行起来

安装64位的python后(1)中的问题才被正常解决。

补充1:

后续测试又发现,使用32位的python访问windows命令时,会被自动重定向到C:\Windows\SysWOW64文件夹中(32位的命令),如果在C:\Windows\SysWOW64文件夹中没有找到相关的命令,就会报(1)中的错误。

如果想在32位的python中直接访问C:\Windows\System32文件夹中64位的命令,则应该使用C:\Windows\SysNative这个虚拟路径来访问。

举例:在32位的python中使用os.system      访问 C:\Windows\System32\netcfg.exe  ----> 系统实际上会去访问 C:\Windows\SysWOW64\netcfg.exe

                                                                      访问 C:\Windows\SysNative\netcfg.exe ----> 系统实际上会去访问 C:\Windows\System32\netcfg.exe

参考资料:

(1)https://stackoverflow.com/questions/5144402/nothing-returned-when-using-32-bit-python-os-popen-on-a-64-bit-windows-7-system

(2)http://leonax.net/p/2601/magic-of-sysnative-folder/



阅读全文
0 0
原创粉丝点击