无需更改 python 代码中的目录,只需将文件名定义为绝对路径(完整路径)。因此,而不是例如File1.pdf
或MyFolder/File1.pdf
相对的,将其更改为绝对的例如/home/ptklearning/Documents/MyFolder/File1.pdf
或C:\Users\ptklearning\Documents\MyFolder\File1.pdf
或无论它在哪里。
示例代码:
merger = bytes()
filenames = [
'/home/nponcian/Documents/GitHub/myproject/src/notes/file1.py',
'/home/nponcian/Documents/Program/file2.txt',
# Add more files as you wish. Note that they would be appended as raw bytes.
]
filename_output = "document-output.txt"
for filename in filenames:
with open(filename, 'rb') as file_input:
file_content = file_input.read()
merger += file_content
with open(filename_output, 'wb') as file_output:
file_output.write(merger)
document-output.txt 的内容:
# This is file1.py
# A dummy line within file1.py
While this is file2.txt!
Nothing more from this 2nd file...
file2 wants to say goodbye now!
示例代码(使用 PDF 文件):
from PyPDF2 import PdfFileMerger, PdfFileReader
merger = PdfFileMerger()
filenames = [
'/home/nponcian/Documents/Program/StackOverflow_how_to_python.pdf',
'/media/sf_VirtualBoxFiles/python_cheat_sheet.pdf',
# Add more files as you wish. Note that they would be appended as PDF files.
]
filename_output = "document-output.pdf"
for filename in filenames:
merger.append(PdfFileReader(filename, strict=False))
with open(filename_output, 'wb') as file_output:
merger.write(file_output)
document-output.pdf 的内容:
<Combined PDF of the input files>
笔记:
rb
表示读取二进制
wb
表示 Write-Binary (如果您想始终覆盖输出文件)
ab
表示附加二进制(如果您只想将新合并附加到输出文件)
如果您正在处理的文件不是通常的文本文件,例如 docx、pdf、mp3 等,则需要显式b
(二进制)。尝试使用文本编辑器打开它们,您就会明白我的意思 :) 此类文件将被读取作为一个python-bytes
对象而不是作为python-str
对象。