Subprocess in Python: File Name too long

前端 未结 2 696
臣服心动
臣服心动 2021-02-19 20:36

I try to call a shellscript via the subprocess module in Python 2.6.

import subprocess

shellFile = open(\"linksNetCdf.txt\", \"r\")

for row in shellFile:
    s         


        
相关标签:
2条回答
  • 2021-02-19 21:16

    subprocess.call can take the command to run in two ways - either a single string like you'd type into a shell, or a list of the executable name followed by the arguments.

    You want the first, but were using the second

    import subprocess
    
    shellFile = open("linksNetCdf.txt", "r")
    
    for row in shellFile:
        subprocess.call(row, shell=True)
    

    By converting your row into a list containing a single string, you're saying something like "Run the command named echo these were supposed to be arguments with no arguments"

    0 讨论(0)
  • 2021-02-19 21:23

    You need to tell subprocess to execute the line as full command including arguments, not just one program.

    This is done by passing shell=True to call

     import subprocess
     cmd = "ls " + "/tmp/ " * 30
     subprocess.call(cmd, shell=True)
    
    0 讨论(0)
提交回复
热议问题