title: Path Problems (command line)
body: <p>I have created a script with an array containing file names. The script searches for pdf files through directories and sub-directories by recursion and adds them to an array. It then outputs a string into the command line for pdftk so as to merge them.</p>
  
  <p>pdftk takes arguments such as:</p>
  
  <pre><code>pdftk inputpdf1.pdf inputpdf2.pdf cat output output.pdf
  </code></pre>
  
  <p>However, it seems that the inputted path is not correct as per the error message I get from the windows cmd (listed above). I get the same error on Ubuntu.</p>
  
  <pre><code>    Microsoft Windows XP [Version 5.1.2600]
  (C) Copyright 1985-2001 Microsoft Corp.
  
  C:\Documents and Settings\student3&gt;cd C:\Documents and Settings\student3\Desktop
  \Test
  
  C:\Documents and Settings\student3\Desktop\Test&gt;pdftest.py
  Merging C:\Documents and Settings\student3\Desktop\Test\1.pdf
  pdftk "C:\Documents and Settings\student3\Desktop\Test\1.pdf" cat outputC:\Docum
  ents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  pdftk "C:\Documents and Settings\student3\Desktop\Test\1.pdf" cat outputC:\Docum
  ents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  Merging C:\Documents and Settings\student3\Desktop\Test\2.pdf
  pdftk "C:\Documents and Settings\student3\Desktop\Test\2.pdf" cat outputC:\Docum
  ents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  pdftk "C:\Documents and Settings\student3\Desktop\Test\2.pdf" cat outputC:\Docum
  ents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  Merging C:\Documents and Settings\student3\Desktop\Test\brian\1.pdf
  pdftk "C:\Documents and Settings\student3\Desktop\Test\brian\1.pdf" cat outputC:
  \Documents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  pdftk "C:\Documents and Settings\student3\Desktop\Test\brian\1.pdf" cat outputC:
  \Documents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  Merging C:\Documents and Settings\student3\Desktop\Test\brian\2.pdf
  pdftk "C:\Documents and Settings\student3\Desktop\Test\brian\2.pdf" cat outputC:
  \Documents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  pdftk "C:\Documents and Settings\student3\Desktop\Test\brian\2.pdf" cat outputC:
  \Documents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  Merging C:\Documents and Settings\student3\Desktop\Test\testing\1.pdf
  pdftk "C:\Documents and Settings\student3\Desktop\Test\testing\1.pdf" cat output
  C:\Documents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  pdftk "C:\Documents and Settings\student3\Desktop\Test\testing\1.pdf" cat output
  C:\Documents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  Merging C:\Documents and Settings\student3\Desktop\Test\testing\2.pdf
  pdftk "C:\Documents and Settings\student3\Desktop\Test\testing\2.pdf" cat output
  C:\Documents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  pdftk "C:\Documents and Settings\student3\Desktop\Test\testing\2.pdf" cat output
  C:\Documents and Settings\student3\Desktop\Test\Output\.pdf
  Error: Unexpected text in page reference, here:
     outputC:\Documents
     Exiting.
     Acceptable keywords, here, are: "even", "odd", or "end".
  Errors encountered.  No output created.
  Done.  Input errors, so no output created.
  Finished Processing
  
  C:\Documents and Settings\student3\Desktop\Test&gt;
  </code></pre>
  
  <p>This is the code for the script:</p>
  
  <pre><code>#----------------------------------------------------------------------------------------------
  # Name:        pdfMerger
  # Purpose:     Automatic merging of all PDF files in a directory and its sub-directories and
  #              rename them according to the folder itself. Requires the pyPDF Module
  #
  # Current:     Processes all the PDF files in the current directory
  # To-Do:       Process the sub-directories.
  #
  # Version: 1.0
  # Author:      Brian Livori
  #
  # Created:     03/08/2011
  # Copyright:   (c) Brian Livori 2011
  # Licence:     Open-Source
  #---------------------------------------------------------------------------------------------
  #!/usr/bin/env python
  
  import os
  import glob
  import sys
  import fnmatch
  import subprocess
  
  path = str(os.getcwd())
  
  
  x = 0
  
  def process_file(_, path, filelist):
      os.path.walk(os.path.realpath(topdir), process_file, ())
      input_param = " ".join('"' + x + '"' for x in glob.glob(os.path.join(path, "*.pdf"))
  
      output_param = '"' + os.path.join(path, os.path.basename(path) + ".pdf") + '"'
  
      cmd = "pdftk " + input_param + " cat output " + output_param
      os.system(cmd)
  
  
      for filenames in os.walk (path):
           if "Output" in filenames:
              filenames.remove ("Output")
  
      if os.path.exists(final_output) != True:
  
                      os.mkdir(final_output)
                      sp = subprocess.Popen(cmd)
                      sp.wait()
  
  
      else:
  
                     sp = subprocess.Popen(cmd)
                     sp.wait()
  
  
  
  
  def files_recursively(topdir):
   os.path.walk(os.path.realpath(topdir), process_file, ())
  
  files_recursively(path)
  
  print "Finished Processing"
  </code></pre>
  
  <p>What exactly am I doing wrong?</p>
  
  <pre><code>File "C:\Documents and Settings\student3\Desktop\Test\pdftest2.py", line 32
      output_param = '"' + os.path.join(path, os.path.basename(path) + ".pdf") + '"'
                 ^
  SyntaxError: invalid syntax
  </code></pre>