'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. Now for my first question ever.
I am attempting to use echo to pass a value to a device, specifically /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.
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.
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. When running the command in a shell directly it works just fine.
echo 1 > /dev/hddled1
I am not sure where to go next as I have run out of things to try and look up. Any help that anyone can provide would be very appreciated.
Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
