Проблемы с путями (командная строка)
Я создал скрипт с массивом, содержащим имена файлов. Скрипт ищет 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 ответа
Вам нужно избегать путей, заключая их в двойные кавычки, из-за пробелов. В противном случае ваша оболочка будет интерпретировать каждый пробел как разделитель для нового файла.
" ".join('"' + str(f) + '"' for f in filesArr)
Еще несколько вещей:
- Вы звоните PDFTK для каждого PDF. Вы должны вывести это из цикла и создать входной список файлов. (Предполагая, что вы хотите объединить все входные PDF-файлы в один выходной PDF-файл
Вам не хватает пробела после вывода cat
... " cat output " + outputpath + ext)
Ваш
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"
Вместо 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()