如何通过Python运行“Windows功能”?

时间:2016-07-26 15:54:46

标签: python windows command-line

我正在尝试从python脚本运行tftp。我启用它作为Windows 10功能(http://www.trishtech.com/2014/10/enable-tftp-telnet-in-windows-10/),所以我可以通过命令行运行它。但是当我尝试通过python运行它时,我得到了错误。我尝试了3种调用它的方法:os.system,subprocess.call,subprocess.Popen,并且作为“控件”使用这些子进程成功调用'dir'。我也尝试从完整路径调用tftp。代码和输出粘贴在下面。我添加了延迟和一些打印来帮助解析。

我的猜测是,在使用这些方法时,我启用的其他“Windows功能”不是Python看到的环境的一部分。但我不确定如何指定环境以包含该功能!

call("dir", shell = True)
print "*******************************************"
time.sleep(1)
call("tftp", shell = True)
print "*******************************************"
time.sleep(1)
call("C:\Windows\System32\\tftp.exe", shell = True)
print "*******************************************"
time.sleep(1)
Popen("dir", shell = True)
print "*******************************************"
time.sleep(1)
Popen("tftp", shell = True)
print "*******************************************"
time.sleep(1)
Popen("C:\Windows\System32\\tftp.exe", shell = True)
print "*******************************************"
time.sleep(1)
os.system("dir")
print "*******************************************"
time.sleep(1)
os.system("tftp")
print "*******************************************"
time.sleep(1)
os.system("C:\Windows\System32\\tftp.exe")
print "*******************************************"
time.sleep(1)

我得到的输出是:

Directory of C:\[REDACTED]

07/26/2016  11:05 AM    <DIR>          .
07/26/2016  11:05 AM    <DIR>          ..
07/26/2016  11:05 AM             1,724 [REDACTED].py
07/26/2016  11:05 AM             1,828 [REDACTED].pyc
07/23/2016  05:36 PM             1,933 prep_for_bootload.py
07/26/2016  07:52 AM                13 s.bat
07/26/2016  11:19 AM             1,449 scrap.py
07/26/2016  10:38 AM               672 test_script.py
07/26/2016  10:39 AM                90 upload_script.bat
               7 File(s)          7,709 bytes
               2 Dir(s)  222,286,884,864 bytes free
*******************************************
'tftp' is not recognized as an internal or external command,
operable program or batch file.
*******************************************
'C:\Windows\System32\tftp.exe' is not recognized as an internal or external command,
operable program or batch file.
*******************************************
*******************************************
 Volume in drive C has no label.
 Volume Serial Number is 2AC7-8D0E

 Directory of C:\Users\[REDACTED]

07/26/2016  11:05 AM    <DIR>          .
07/26/2016  11:05 AM    <DIR>          ..
07/26/2016  11:05 AM             1,724 [REDACTED]
07/26/2016  11:05 AM             1,828 [REDACTED]
07/23/2016  05:36 PM             1,933 prep_for_bootload.py
07/26/2016  07:52 AM                13 s.bat
07/26/2016  11:19 AM             1,449 scrap.py
07/26/2016  10:38 AM               672 test_script.py
07/26/2016  10:39 AM                90 upload_script.bat
               7 File(s)          7,709 bytes
               2 Dir(s)  222,286,884,864 bytes free
*******************************************
'tftp' is not recognized as an internal or external command,
operable program or batch file.
*******************************************
'C:\Windows\System32\tftp.exe' is not recognized as an internal or external command,
operable program or batch file.
 Volume in drive C has no label.
 Volume Serial Number is 2AC7-8D0E

 Directory of C:\Users\[REDACTED]

07/26/2016  11:05 AM    <DIR>          .
07/26/2016  11:05 AM    <DIR>          ..
07/26/2016  11:05 AM             1,724 [REDACTED]
07/26/2016  11:05 AM             1,828 [REDACTED]
07/23/2016  05:36 PM             1,933 prep_for_bootload.py
07/26/2016  07:52 AM                13 s.bat
07/26/2016  11:19 AM             1,449 scrap.py
07/26/2016  10:38 AM               672 test_script.py
07/26/2016  10:39 AM                90 upload_script.bat
               7 File(s)          7,709 bytes
               2 Dir(s)  222,286,884,864 bytes free
*******************************************
'tftp' is not recognized as an internal or external command,
operable program or batch file.
*******************************************
'C:\Windows\System32\tftp.exe' is not recognized as an internal or external command,
operable program or batch file.
*******************************************

1 个答案:

答案 0 :(得分:0)

请参阅:@eryksun的回复以获得正确答案。引用这里:

&#34;要在任何一种情况下工作,请使用system32 =&#39; SysNative&#39;如果platform.architecture()[0] ==&#39; 32bit&#39;否则&#39; System32&#39;。然后创建完全限定的路径为tftp_path = os.path.join(os.environ [&#39; SystemRoot&#39;],system32,&#39; TFTP.EXE&#39;)&#34;