[英]Python subprocess.run not working but shows completed
Hello first thanks to everyone who contributes.您好,首先感谢所有做出贡献的人。 As a new to python self learner you have all saved me alot of time.作为 python 自学者的新手,你们都为我节省了很多时间。 Now for my first question ever.现在是我的第一个问题。
I am attempting to use echo to pass a value to a device, specifically /dev/hddled#.我正在尝试使用 echo 将值传递给设备,特别是 /dev/hddled#。 I am attempting to do this in Python using the following script, I understand it is not very clean I am still learning.我正在尝试使用以下脚本在 Python 中执行此操作,我知道它不是很干净我仍在学习中。
import sys
import re
import subprocess
disk1 = '[0:0:0:0]'
disk2 = '[1:0:0:0]'
disk3 = '[2:0:0:0]'
disk4 = '[3:0:0:0]'
disk5 = '[4:0:0:0]'
patterns = ['/dev/sda', '/dev/sdb','/dev/sdc','/dev/sdd','/dev/sde']
drives = []
gettingtext = subprocess.check_output(['lsscsi'])
mytest = subprocess.check_output(['lsscsi']).decode("utf-8")
print(mytest)
print(type(mytest))
lines = mytest.splitlines()
# f = open("lsscsi.txt", "r")
for line in lines:
if disk1 in line:
searchdsk = re.findall(r'\W/.+',line)
drives.append('1')
print(str(drives))
elif disk2 in line:
searchdsk = re.findall(r'\W/.+', line)
drives.append('2')
print(str(drives))
elif disk3 in line:
searchdsk = re.findall(r'\W/.+', line)
drives.append('3')
print(str(drives))
elif disk4 in line:
searchdsk = re.findall(r'\W/.+', line)
drives.append('4')
print(str(drives))
elif disk5 in line:
searchdsk = re.findall(r'\W/.+', line)
drives.append('5')
print(str(drives))
print("These are the drives: " + str(drives))
print(type(drives))
for disknum in drives:
stuff = subprocess.run(["/usr/bin/echo", "1", ">", "/dev/hddled" + disknum], shell=True, text=True, capture_output=True)
stuff
print("stdout", stuff.stdout)
print("stderr", stuff.stderr)
print(type(disknum))
print("This is number: " + disknum)
print("This is the command:" + str(stuff))
# f.close()
When I run this code it seems to work just fine and I am getting back the expected values for the drives list.当我运行这段代码时,它似乎工作得很好,我得到了驱动器列表的预期值。 I am also receiving Completed processes with a returncode=0 and the stdout='\n' which is expected.我还收到了 Completed processes with a returncode=0 和 stdout='\n' 这是预期的。
This is the command:CompletedProcess(args=['/usr/bin/echo', '1', '>', '/dev/hddled5'], returncode=0, stdout='\n', stderr='')
However it does not appear to be echoing the value to the /dev/device.但是它似乎没有将值回显到 /dev/device。 When running the command in a shell directly it works just fine.当直接在 shell 中运行命令时,它工作得很好。
echo 1 > /dev/hddled1
I am not sure where to go next as I have run out of things to try and look up.我不确定下一步去哪里 go 因为我已经没有东西可以尝试查找了。 Any help that anyone can provide would be very appreciated.任何人都可以提供的任何帮助将不胜感激。
Thanks谢谢
If you use shell=True
, you must only pass a single string argument as the first argument to subprocess.run
.如果使用shell=True
,则必须仅将单个字符串参数作为第一个参数传递给subprocess.run
。 When you write:当你写:
stuff = subprocess.run(["/usr/bin/echo", "1", ">", "/dev/hddled" + disknum], shell=True, text=True, capture_output=True)
You are in fact only running /usr/bin/echo
.您实际上只是在运行/usr/bin/echo
。 You should write instead:你应该这样写:
stuff = subprocess.run("/usr/bin/echo 1 > /dev/hddled" + disknum, shell=True, text=True, capture_output=True)
Although I would suggest a format string instead of string concatention (just because I think it's a little more obvious what's going on):尽管我建议使用格式字符串而不是字符串连接(只是因为我认为发生的事情更明显):
stuff = subprocess.run(f"/usr/bin/echo 1 > /dev/hddled{disknum}", shell=True, text=True, capture_output=True)
Of course, instead of calling out to echo
, you could also just do this in Python. Something like this should be equivalent:当然,除了调用echo
之外,您也可以在 Python 中执行此操作。这样的事情应该是等价的:
with open(f"/dev/hddled{disknum}") as fd:
fd.write('1\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.