KnowledgeBoat Logo
|

Computer Science

To open a file c:\ss.txt for appending data, we use

  1. file = open("c:\\ss.txt", "a")
  2. file = open("c:\\ss.txt", "rw")
  3. file = open(r"c:\ss.txt", "a")
  4. file = open(file = "c:\ss.txt", "w")
  5. file = open(file = "c:\ss.txt", "w")
  6. file = open("c:\res.txt")

Python File Handling

4 Likes

Answer

file = open("c:\\ss.txt", "a")
file = open(r"c:\ss.txt", "a")

Reason —

  1. file = open("c:\\ss.txt", "a") — The syntax to open a file c:\ss.txt is f = open("c:\\temp\\data.txt", "a"). Hence according to this syntax file = open("c:\\ss.txt", "a") is correct format.
  2. file = open(r"c:\ss.txt", "a") — The syntax to open a file c:\ss.txt with single slash is f = open(r"c:\temp\data.txt","a").The prefix r in front of a string makes it raw string that means there is no special meaning attached to any character. Hence according to this syntax file = open(r"c:\ss.txt", "a") is correct format.

Answered By

1 Like


Related Questions