title: What is the subprocess.Popen max length of the args parameter?
body: <p>I am using <a href="http://docs.python.org/library/subprocess.html#using-the-subprocess-module" rel="noreferrer">Popen</a> function from the subprocess module to execute a command line tool:</p>
  
  <pre><code>subprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
  </code></pre>
  
  <p>The tool I am using takes a list of files that it then processes. In some cases, this list of files can be very long. Is there a way to find the max length that the args parameter can be? With a large number of files being passed to the tool, I am getting the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "dump_output_sopuids.py", line 68, in &lt;module&gt;
      uid_map = create_sopuid_to_path_dict_dcmdump(dicom_files)
    File "dump_output_sopuids.py", line 41, in create_sopuid_to_path_dict_dcmdump
      dcmdump_output = subprocess.Popen(cmd,stdout=subprocess.PIPE).communicate(0)[0]
    File "c:\python26\lib\subprocess.py", line 621, in __init__
      errread, errwrite)
    File "c:\python26\lib\subprocess.py", line 830, in _execute_child
      startupinfo)
  WindowsError: [Error 206] The filename or extension is too long
  </code></pre>
  
  <p>Is there a general way to find this max length? I found the following article on msdn: <a href="http://support.microsoft.com/kb/830473" rel="noreferrer">Command prompt (Cmd. exe) command-line string limitation</a> but I don't want to hard code in the value. I would rather get the value at run time to break up the command into multiple calls.</p>
  
  <p>I am using Python 2.6 on Windows XP 64.</p>
  
  <p>Edit: adding code example</p>
  
  <pre><code>paths = ['file1.dat','file2.dat',...,'fileX.dat']
  cmd = ['process_file.exe','+p'] + paths
  cmd_output = subprocess.Popen(cmd,stdout=subprocess.PIPE).communicate(0)[0]
  </code></pre>
  
  <p>The problem occurs because each actual entry in the <code>paths</code> list is usually a very long file path AND there are several thousand of them.</p>
  
  <p><em>I don't mind breaking up the command into multiple calls to <code>process_file.exe</code>. I am looking for a general way to get the max length that args can be so I know how many paths to send in for each run.</em></p>