Subprocess in Python: File Name too long

2020-07-03 09:45发布

问题:

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:
    subprocess.call([str(row)])

My filenames have a length ranging between 400 and 430 characters. When calling the script I get the error:

File "/usr/lib64/python2.6/subprocess.py", line 444, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.6/subprocess.py", line 595, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1106, in _execute_child
raise child_exception
OSError: [Errno 36] File name too long

An example of the lines within linksNetCdf.txt is

./ShellScript 'Title' 'Sometehing else' 'InfoInfo' 'MoreInformation' inputfiile outputfile.txt 3 2

Any ideas how to still run the script?

回答1:

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"



回答2:

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)


标签: python shell