我正在使用subprocess.check_output()通过Sysinternals PSExec在远程计算机上执行一些命令。我遇到的问题是,如果命令给出非零的返回码,则check_output()会引发CalledProcessError。
PSExec将创建的进程的PID作为其返回代码,因此很明显每次都会出现错误。有没有一种方法可以覆盖此错误并仍然检查psexec调用的输出?基本上,我只需要查看输出,即使其返回码为非零也是如此。
谢谢!
谁能解释一下这里发生了什么?即使内置cmd.exe命令也无法正常工作:
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call('dir')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Python34\lib\subprocess.py", line 535, in call
with Popen(*popenargs, **kwargs) as p:
File "D:\Python34\lib\subprocess.py", line 848, in __init__
restore_signals, start_new_session)
File "D:\Python34\lib\subprocess.py", line 1104, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>>
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写python代码,它将在linux机器的cmd终端中执行以下步骤
cd /path/to/destination/
3dAFNItoNIFTI *epi*
Run Code Online (Sandbox Code Playgroud)
我试过subprocess.call两次,一次cd,然后执行该命令.但似乎没有用.
有没有办法使用子进程调用两行inux命令代码?
谢谢!
我是Python的新手.当我执行以下命令以使用以下python代码获取我的x86服务器上的总CPU核心数时,我得到如下错误;
########################Code#######################
#!/usr/bin/python
import os
import re
import subprocess
os.system('clear')
#CPU core count check
flag=0
p = subprocess.Popen(['cat /proc/cpuinfo | grep -i processor | wc -l'], stdout=subprocess.PIPE)
print p.communicate(1)
##############################Output####################
Traceback (most recent call last):
File "./chk_config.py", line 9, in <module>
p = subprocess.Popen(['cat /proc/cpuinfo | grep -i processor | wc -l'], stdout=subprocess.PIPE)
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Run Code Online (Sandbox Code Playgroud)
os.popen工作,但我不想使用,os.popen …
我正在尝试获取bash命令的标准输出作为Python中的字符串。根据Popen文档,我尝试过:
import subprocess
p = subprocess.Popen(["echo", "hello"])
stdoutdata, stderrdata = p.communicate()
print stdoutdata
Run Code Online (Sandbox Code Playgroud)
运行此脚本将产生以下输出:
import subprocess
p = subprocess.Popen(["echo", "hello"])
stdoutdata, stderrdata = p.communicate()
print stdoutdata
Run Code Online (Sandbox Code Playgroud)
因此,尽管输出已通过Python打印,但stdoutdata变量为None,而不是"hello"我想要的。我该怎么做?
我有以下(伪)代码:
import os
for s in settings:
job_file = open("new_file_s.sh", "w")
job_file.write("stuff that depends on s")
os.system(command_that_runs_file_s)
Run Code Online (Sandbox Code Playgroud)
不幸的是,发生的事情是对应的文件s = settings[0]没有执行,但随后s = settings[1]执行.显然,os.system()不喜欢运行最近使用的文件open(),特别是在for循环的同一次迭代中.
对我来说,修复是确保os.system()在for循环的先前迭代中初始化任何执行的文件:
import os
# Stagger so that writing happens before execution:
job_file = open("new_file_settings[0].sh", "w")
job_file.write("stuff that depends on settings[0]")
for j in range(1, len(settings)):
job_file = open("new_file_settings[j].sh", "w")
job_file.write("stuff that depends on settings[j]")
# Apparently, running a file in the same iteration of a for loop is …Run Code Online (Sandbox Code Playgroud) 我正在使用以下子流程调用来使用命令行工具。命令行工具的输出不是一次性打印的,而是立即在命令行上打印的,它会在一段时间内生成多行。该工具为bs1770gain,命令为“ path \ to \ bs1770gain.exe”“ -i”“ \ path \ to \ audiofile.wav”,通过使用--loglevel参数,您可以包含更多数据,但不能删除渐进结果被写入标准输出。
我需要stdout返回人类可读的字符串(因此stdout_formatted操作):
with subprocess.Popen(list_of_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
stdout, stderr = proc.communicate()
stdout_formatted = stdout.decode('UTF-8')
stderr_formatted = stderr.decode('UTF-8')
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其打印,则只能将其视为人类可读的字符串,例如
In [23]: print(stdout_formatted )
nalyzing ... [1/2] "filename.wav":
integrated: -2.73 LUFS / -20.27 LU [2/2]
"filename2.wav":
integrated: -4.47 LUFS / -18.53 LU
[ALBUM]:
integrated: -3.52 LUFS / -19.48 LU done.
In [24]: stdout_formatted
Out[24]: 'a\x00n\x00a\x00l\x00y\x00z\x00i\x00n\x00g\.......
In [6]: stdout
Out[6]: b'a\x00n\x00a\x00l\x00y\x00z\x00i\x00n\x00g\......
In [4]: type(stdout)
Out[4]: bytes
In [5]: …Run Code Online (Sandbox Code Playgroud) 操作系统:Windows 10 Python:3.5.2我试图打开calc.exe做一些动作,然后关闭它。这是我的代码示例
import subprocess, os, time
p = subprocess.Popen('calc.exe')
#Some actions
time.sleep(2)
p.kill()
Run Code Online (Sandbox Code Playgroud)
因此,这对于calc.exe不起作用,它只是打开计算器,但没有关闭它,但是对于“ notepad.exe”来说,相同的代码工作正常。
我猜在子进程库中有一个用于进程终止方法的错误。因此,任务管理器中的notepad.exe进程名称为notepad.exe,但是calc.exe进程名称为Calculator.exe,因此,我猜测它正在尝试按名称杀死它并找不到它。
这不起作用,并说没有这样的文件或目录
current_conecctions = subprocess.Popen("netstat -p udp",shell = False, stdout=subprocess.PIPE).stdout.read()
Run Code Online (Sandbox Code Playgroud)
虽然netstat -p udp在终端中完美运行.
最近,我尝试制作一个新的Key logger,并使用这段代码将其作为后台进程运行:
DETACHED_PROCESS = 0x00000008
pid = subprocess.Popen([sys.executable, "KeyLogger.py"],
creationflags=DETACHED_PROCESS).pid
Run Code Online (Sandbox Code Playgroud)
当我将其作为python文件运行时,我的代码正常工作,但当我使用以下命令将其转换为.exe文件时:
pyinstaller --onefile myfile.py
Run Code Online (Sandbox Code Playgroud)
它不再起作用了.但是当我删除这段代码时:
DETACHED_PROCESS = 0x00000008
pid = subprocess.Popen([sys.executable, "KeyLogger.py"],
creationflags=DETACHED_PROCESS).pid
Run Code Online (Sandbox Code Playgroud)
它工作正常.现在,我正在寻找一种新方法来运行我的python代码作为后台进程(如.exe),而不显示任何控制台窗口. 另外,我很抱歉在我的问题中写错了.
subprocess ×10
python ×9
python-3.x ×3
ascii ×1
bash ×1
keylogger ×1
linux ×1
netstat ×1
os.system ×1
psexec ×1
pyinstaller ×1
python-2.7 ×1
shell ×1
string ×1
utf-8 ×1
windows ×1