title: Try except in file opening
body: <p>I wrote a program to open a file and print every single line if the file is not there create it and write some line to it </p>
  
  <p>it works perfectly when the file is not present in the directory 
  but when the file is there it opens the file reading it -->and then writing it to the file but it should only read the file </p>
  
  <pre><code>print("We will do some file operation")
  filename=input("enter a file name: ")
  try:
   file=open(filename,'r')
   print("File opened")
   for line in file:
      print(line )
   filename.close()
   print("file cosed")
  
  except:
   file=open(filename,'w')
   print("File created")
   for i in range(15):
      file.write("This is line %d\r\n"%(i))
   print("write operation done")
   file.close()
  </code></pre>