python - Python 中的子进程 : File Name too long

标签 python shell

我尝试通过 Python 2.6 中的子进程模块调用 shellscript。

import subprocess

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

for row in shellFile:
    subprocess.call([str(row)])

我的文件名长度在 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

linksNetCdf.txt 中的行示例是

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

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

最佳答案

subprocess.call 可以采用两种方式运行命令 - 可以是您在 shell 中键入的单个字符串,也可以是后跟参数的可执行文件名称列表。

你想要第一个,但正在使用第二个

import subprocess

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

for row in shellFile:
    subprocess.call(row, shell=True)

通过将您的转换为包含单个字符串的列表,您说的是类似“运行名为echo these were supposed to be arguments的命令没有参数”

关于python - Python 中的子进程 : File Name too long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29747108/

相关文章:

linux - 如何在shell脚本中提取字符串

python - 使用Python请求发布JSON

javascript - 在 python-vtk/three.js 中居中多数据模型的轴/枢轴

python不能返回变量但可以打印

shell - Powershell:如何在无休止的脚本行之后继续运行脚本?

bash - 在后台 Python 脚本完成之前防止关机/重启

linux - 我怎样才能将我所有的 web 节点从一个 master 上同步?

python - 在Python的re中,如果子字符串在开头或中间匹配,如何获取字符串?

python - Pandas 中的时间分级

c - 如何使 Linux 中的脚本使用我的拦截器并工作? (#!)