【发布时间】:2016-07-26 15:54:46
【问题描述】:
我正在尝试从 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.
*******************************************
【问题讨论】:
-
您是否尝试过定位
tftp命令所在的位置,然后从完整位置手动调用它?即,如果它安装到c:\windows\tftp.exe,则使用它的完整路径从python 调用它。 -
是的,我也尝试过 - 将其添加到我的 OP 中
-
"\t"是制表符,而不是后跟“t”的反斜杠。 -
使用本机实现而不是分叉是一种很好的做法。它将提供更好的集成和更好的错误处理。见pypi.python.org/pypi/tftpy
-
请提供您使用的确切代码。见minimal reproducible example
标签: python windows command-line