Python Forum
open("python.pdf","wb") - why "wb" instead of "a"?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
open("python.pdf","wb") - why "wb" instead of "a"?
#1
I came across the following code in a website.

import requests 

file_url = "http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/ch1-2.pdf"

  

r = requests.get(file_url, stream = True) 

  

with open("python.pdf","wb") as pdf: 

    for chunk in r.iter_content(chunk_size=1024): 

  

         # writing one chunk at a time to pdf file 

         if chunk: 

             pdf.write(chunk) 
Now the file is being written in chunks. So why it is opened as "w" (write mode), instead of "a" (append mode) because if we open the file as "w" we know that when the latest chunk will be written it will overwrite the file and we will loose the previous chunks.
Reply
#2
'wb' -- will open a new file for writing, binary mode
'a' -- will open a new file, but if it already exists, will append to existing file.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020