Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subprocess in Python: File Name too long

Tags:

python

shell

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?

like image 948
Stophface Avatar asked Apr 20 '15 11:04

Stophface


People also ask

What is subprocess popen ()?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

Why are shells true in subprocess?

After reading the docs, I came to know that shell=True means executing the code through the shell. So that means in absence, the process is directly started.

Does Python wait for subprocess run to finish?

Most of your interaction with the Python subprocess module will be via the run() function. This blocking function will start a process and wait until the new process exits before moving on.

What does subprocess Check_call return?

subprocess. check_call() gets the final return value from the script, and 0 generally means "the script completed successfully".


1 Answers

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)
like image 168
deets Avatar answered Sep 16 '22 15:09

deets