How to create the arguments list in subprocess.run

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?

you’re trying to execute shell commands, so you need to pass shell=True to run()

Thanks! I knew I was just missing something simple.

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]

Thanks! Much simpler. I’m just learning python. So, not always making the correct choices. :slight_smile:

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).

Thanks. Yes, I was getting some of that while testing. :slight_smile: Brenainn’s solution worked great!