路径问题(命令行)

时间:2011-08-10 11:50:36

标签: python path cmd pdftk

我创建了一个包含文件名的数组的脚本。脚本通过递归搜索目录和子目录中的pdf文件,并将它们添加到数组中。然后它将一个字符串输出到pdftk的命令行中,以便合并它们。

pdftk采用如下参数:

pdftk inputpdf1.pdf inputpdf2.pdf cat output output.pdf

但是,根据我从windows cmd(上面列出的)获得的错误消息,似乎输入的路径不正确。我在Ubuntu上遇到同样的错误。

    Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\student3>cd C:\Documents and Settings\student3\Desktop
\Test

C:\Documents and Settings\student3\Desktop\Test>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>

这是脚本的代码:

#----------------------------------------------------------------------------------------------
# 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"

我究竟做错了什么?

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

2 个答案:

答案 0 :(得分:3)

由于空格,您需要通过用双引号将它们括起来来转义路径。否则,shell会将每个空格解释为新文件的分隔符。

" ".join('"' + str(f) + '"' for f in filesArr)

还有几件事:

  1. 您为每个PDF调用PDFTK。你应该把它放在循环之外并构建一个文件输入列表。 (假设您要将所有输入pdf合并为一个输出pdf
  2. cat输出后你缺少一个空格

    ... " cat output " + outputpath + ext)

  3. 您的outputpath变量为空。

  4. 编辑:

    您的代码有点令人困惑。我会将process_file方法更改为:

    def process_file(_, path, filelist):
        input_param = " ".join('"' + x + '"' for x in glob.glob(os.path.join(path, "*.pdf"))
        output_param = '"C:\ENTER\OUTPUT\PATH\HERE.PDF"'
        cmd = "pdftk " + input_param + " cat output " + output_param
        os.system(cmd)
    

    我真的不明白为什么你需要那里的所有作业。

    编辑2:

    这是我的完整脚本:

    #!/usr/bin/env python
    
    import os
    import glob
    
    def process_file(_, path, filelist):
        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
        print cmd
        os.system(cmd)
    
    def files_recursively(topdir):
        os.path.walk(os.path.realpath(topdir), process_file, ())
    
    if  __name__ == "__main__":
        files_recursively(os.getcwd())
    

    此处Pastebin

    它产生的命令:

    pdftk "/home/user/pdf/Test1.pdf" "/home/user/pdf/Test3.pdf" "/home/user/pdf/Test2.pdf" cat output "/home/user/pdf/pdf.pdf"
    pdftk "/home/user/pdf/Sub3/Test1.pdf" "/home/user/pdf/Sub3/Test3.pdf" "/home/user/pdf/Sub3/Test2.pdf" cat output "/home/user/pdf/Sub3/Sub3.pdf"
    pdftk "/home/user/pdf/Sub2/Test1.pdf" "/home/user/pdf/Sub2/Test3.pdf" "/home/user/pdf/Sub2/Test2.pdf" cat output "/home/user/pdf/Sub2/Sub2.pdf"
    pdftk "/home/user/pdf/Sub2/SubSub21/Test1.pdf" "/home/user/pdf/Sub2/SubSub21/Test3.pdf" "/home/user/pdf/Sub2/SubSub21/Test2.pdf" cat output "/home/user/pdf/Sub2/SubSub21/SubSub21.pdf"
    pdftk "/home/user/pdf/Sub2/SubSub22/Test1.pdf" "/home/user/pdf/Sub2/SubSub22/Test3.pdf" "/home/user/pdf/Sub2/SubSub22/Test2.pdf" cat output "/home/user/pdf/Sub2/SubSub22/SubSub22.pdf"
    pdftk "/home/user/pdf/Sub1/Test1.pdf" "/home/user/pdf/Sub1/Test3.pdf" "/home/user/pdf/Sub1/Test2.pdf" cat output "/home/user/pdf/Sub1/Sub1.pdf"
    pdftk "/home/user/pdf/Sub1/SubSub2/Test1.pdf" "/home/user/pdf/Sub1/SubSub2/Test3.pdf" "/home/user/pdf/Sub1/SubSub2/Test2.pdf" cat output "/home/user/pdf/Sub1/SubSub2/SubSub2.pdf"
    pdftk "/home/user/pdf/Sub1/SubSub1/Test1.pdf" "/home/user/pdf/Sub1/SubSub1/Test3.pdf" "/home/user/pdf/Sub1/SubSub1/Test2.pdf" cat output "/home/user/pdf/Sub1/SubSub1/SubSub1.pdf"
    

答案 1 :(得分:0)

而不是os.system()你应该使用subprocess.Popen - 如果你将命令和参数作为列表给出,这个模块的内容可以正确处理文件名中的空格。

  

在Windows上:Popen类使用CreateProcess()来执行子级   程序,对字符串进行操作。如果args是一个序列,它将是   使用list2cmdline方法转换为字符串。请注意   并非所有MS Windows应用程序都解释命令行相同   方式:list2cmdline是为使用相同的应用程序而设计的   规则作为MS C运行时。

在您的示例中,那将是

cmd = ["pdftk"] + files_arr + "cat", "output", outputpath + ext]

然后

sp = subprocess.Popen(cmd)
sp.wait()