bradawk1
(Bradawk1)
August 29, 2024, 9:43am
1
I am trying to get the results from
grep vendor_id /proc/cpuinfo | head -1
but keep getting errors. When I put each element above into quotes, I get
grep: |: no such file or directory
grep: head: no such file or directory
I’m really not sure how to break this up?
rgh
(R.Ghetta)
August 29, 2024, 9:58am
2
you’re trying to execute shell commands, so you need to pass shell=True
to run()
bradawk1
(Bradawk1)
August 29, 2024, 10:22am
3
Thanks! I knew I was just missing something simple.
bwoodsend
(Brénainn Woodsend)
August 29, 2024, 10:45am
4
Why use shell code to do something you can achieve in native Python?
import re
with open("/proc/cpuinfo") as f:
vendor_id = re.search(r"vendor_id\s+: (.*)", f.read())[1]
bradawk1
(Bradawk1)
August 29, 2024, 10:57am
5
Thanks! Much simpler. I’m just learning python. So, not always making the correct choices.
cameron
(Cameron Simpson)
August 29, 2024, 10:36pm
6
A note for the future: as a rule, try to avoid using shell=True
.
It is easy to incorrectly construct shell commands. Your example was
totally fixed and safe, but as soon as you start eg inserting filenames
into shell command strings it becomes difficult to do safely (because a
filename might include shell punctuation syntax).
bradawk1
(Bradawk1)
August 30, 2024, 9:05am
7
Thanks. Yes, I was getting some of that while testing. Brenainn’s solution worked great!