问题描述
让我解释一下问题:我得到了一个批处理文件,该文件需要管理员权限。而且我想从python启动此批处理文件,所以我做了一个快捷方式,然后用python运行了此快捷方式,并且可以在:
下正常工作os.startfile(r"C:/MyBatchShortcut.lnk")
但是现在我要改进它:我想读取批处理的输出,以将信息显示到我的python应用程序中。因此,我开始使用subprocess.Popen()代替os.startfile()。我用另一个(更简单的)批处理文件进行了测试,它给出了:
import subprocess
output= subprocess.Popen(
(r"C:/AnotherBatchShortcut.lnk"),stdout=subprocess.PIPE,shell=True).stdout
for line in output:
print(line)
output.close()
我可以得到输出,没问题。但是,神奇的事情发生了,当我尝试好批次时,我得到的输出是:
^C
>>>
仅此而已...这是批处理文件:
@echo off
cd C:/
setlocal EnableDelayedExpansion
pause
set count=1
if exist testproxy.txt.tmp del "testproxy.txt.tmp" /s /f /q>NUL
for /f "tokens=*" %%a in (testproxy.txt) do (
if !count! equ 1 (
set "TIMER=%%a"
)
set /a count+=1
)
setlocal
set file=testproxy.txt
set linetoDelete=1
(for /f "tokens=1* delims=:" %%a in ('findstr /N "^" "%file%" ^|findstr /bv "%linetoDelete%:"') do echo/%%b) > "%file%.tmp
move /y "%file%" "%file%">NUL
:loop
for /F "tokens=*" %%A in (testproxy.txt.tmp) do (
netsh winhttp set proxy %%A
timeout %TIMER%
)
goto :loop
所以我使用setlocal EnableDelayedExpansion,在一开始使用@echo关闭,也许有问题吗?还是在python脚本中使用shell = True,因为当我将其设置为false时,即使使用简单的脚本也不起作用?也许管理员权限要求导致了该错误?我不知道,如果您能帮助我,我会很高兴^^'
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)