Python中的子进程:文件名太长

Sto*_*ace 11 python shell

我尝试通过Python 2.6中的子进程模块调用一个shellcript.

import subprocess

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

for row in shellFile:
    subprocess.call([str(row)])
Run Code Online (Sandbox Code Playgroud)

我的文件名长度介于400到430个字符之间.调用脚本时,我收到错误:

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
Run Code Online (Sandbox Code Playgroud)

内线的一个例子linksNetCdf.txt

./ShellScript 'Title' 'Sometehing else' 'InfoInfo' 'MoreInformation' inputfiile outputfile.txt 3 2
Run Code Online (Sandbox Code Playgroud)

任何想法如何仍然运行脚本?

Eri*_*ric 15

subprocess.call 可以使用命令以两种方式运行 - 或者像键入shell一样的单个字符串,或者是可执行名称后跟参数的列表.

你想要第一个,但是使用第二个

import subprocess

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

for row in shellFile:
    subprocess.call(row, shell=True)
Run Code Online (Sandbox Code Playgroud)

通过将您row转换为包含单个字符串的列表,您会说"运行命名echo these were supposed to be arguments为不带参数的命令"


dee*_*ets 8

您需要告诉子进程执行该行作为包含参数的完整命令,而不仅仅是一个程序.

这是通过将shell = True传递给call来完成的

 import subprocess
 cmd = "ls " + "/tmp/ " * 30
 subprocess.call(cmd, shell=True)
Run Code Online (Sandbox Code Playgroud)