gpt4 book ai didi

python - 路径问题(命令行)

转载 作者:行者123 更新时间:2023-11-30 23:51:03 27 4
gpt4 key购买 nike

我创建了一个脚本,其中包含一个包含文件名的数组。该脚本通过递归方式通过目录和子目录搜索 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

最佳答案

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

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

还有几件事:

  1. 您可以为每个 PDF 调用 PDFTK。您应该将其排除在循环之外并构建文件的输入列表。 (假设您想将所有输入 pdf 合并为一个输出 pdf
  2. cat 输出后缺少一个空格

    ...“cat 输出”+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"

关于python - 路径问题(命令行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7010316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com