路径问题(命令行)

1 投票
2 回答
2020 浏览
提问于 2025-04-16 23:16

我写了一个脚本,这个脚本里有一个数组,里面存放了文件名。这个脚本会通过递归的方式在文件夹和子文件夹中搜索pdf文件,并把找到的文件添加到一个数组里。最后,它会在命令行中输出一个字符串给pdftk,用来合并这些文件。

pdftk接受一些参数,比如:

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

不过,根据我在Windows命令提示符上看到的错误信息,输入的路径好像不对(上面列出了错误信息)。在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

与其使用 os.system(),不如使用 subprocess.Popen。这个模块可以很好地处理文件名中的空格,只要你把命令和参数以列表的形式传递进去。

在Windows系统上,Popen类使用CreateProcess()来执行子程序,这个过程是基于字符串的。如果参数是一个序列,它会通过list2cmdline方法转换成字符串。请注意,并不是所有的Windows应用程序都以相同的方式解释命令行:list2cmdline是为那些遵循与MS C运行时相同规则的应用程序设计的。

在你的例子中,可以这样写:

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

然后再:

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

你需要用双引号把路径括起来,因为路径中有空格。否则,你的命令行会把每个空格当成是新文件的分隔符。

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

还有几件事:

  1. 你对每个PDF都调用了一次PDFTK。应该把这个放到循环外面,先把所有文件的输入列表准备好。(假设你是想把所有输入的PDF合并成一个输出的PDF)
  2. 你在“cat output”后面缺少一个空格。

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

  3. 你的outputpath变量是空的。

编辑:

你的代码有点让人困惑。我会把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"

撰写回答