I downloaded a series of pdf files and I want to join them. I am aware of PyPDF or similar modules, but I want to know why I cannot use file.write() method for joining pdf files.
Here is the code I used to download pdf files.
for i in range(3):
url = 'http://ncert.nic.in/ncerts/l/leph10{}.pdf'.format(i+1)
response = requests.get(url)
with open ('file{}.pdf'.format(i+1), 'wb') as file:
for chunk in response.iter_content(chunk_size= 1024):
file.write(chunk)
Then I used following code to join them.
with open ('combined.pdf', 'ab') as combined:
for i in range(2,-1,-1 ):
with open ('file{}.pdf'.format(i+1), 'rb') as file:
for chunk in file:
combined.write(chunk)
The combined file contains only the first file, but not the remaining two files. However, the size of combined file is sum of size of all three files.
I searched through many blogs/questions here to find answers, but everyone seems to suggest PyPDF or similar modules for dealing with PDFs in Python.
My questions are:
i) Why is code joining/appending from the first file only, even though the actual size of combined file is much bigger. I am not getting any exceptions/errors.
ii) Why can I not join pdf files using such simple write() method in Python?