File not found error when launching a subprocess containing piped commands

前端 未结 3 781
南旧
南旧 2020-12-05 01:55

I need to run the command date | grep -o -w \'\"+tz+\"\'\' | wc -w using Python on my localhost. I am using subprocess module for the same and usi

3条回答
  •  庸人自扰
    2020-12-05 02:04

    The most common cause of FileNotFound with subprocess, in my experience, is the use of spaces in your command. Use a list, instead.

    # Wrong, even with a valid command string
    subprocess.run(["date | grep -o -w '\"+tz+\"' | wc -w"])
    
    # Fixed
    subprocess.run(["date", "|", "grep", "-o", "-w", "'\"+tz+\"'", "|", "wc", "-w"])
    

    This change results in no more FileNotFound errors, and is a nice solution if you got here searching for that exception with a simpler command. If you are using python 3.5 or greater, try using this approach:

    import subprocess
    
    a = subprocess.run(["date"], stdout=subprocess.PIPE)
    print(a.stdout.decode('utf-8'))
    
    b = subprocess.run(["grep", "-o", "-w", "'\"+tz+\"'"],
                       input=a.stdout, stdout=subprocess.PIPE)
    print(b.stdout.decode('utf-8'))    
    
    c = subprocess.run(["wc", "-w"],
                       input=b.stdout, stdout=subprocess.PIPE)
    print(c.stdout.decode('utf-8'))
    

    You should see how one command's output becomes another's input just like using a shell pipe, but you can easily debug each step of the process in python. Using subprocess.run is recommended for python > 3.5, but not available in prior versions.

提交回复
热议问题