Advertisement
brilliant_moves

WriteDataFile.py

Oct 4th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. #WriteDataFile.py
  2. #Python3.4
  3. #Chris Clarke
  4. #04.10.2015
  5.  
  6. import sys
  7.  
  8. filename = input( "Enter name of file to create: ")
  9.  
  10. try:
  11.     fh = open( filename, "r")
  12.     fh.close()
  13. except IOError:
  14.     pass
  15. else:
  16.     overwrite = input( "File exists.  Overwrite (y/n) ? ")
  17.     if overwrite.lower() in ("y", "yes"):
  18.         pass
  19.     else:
  20.         print( "Overwrite cancelled." )
  21.         sys.exit()
  22.  
  23. try:
  24.     fdata = open( filename, "w" ) # open text file to write to
  25.     x = 0
  26.     while x>=0:
  27.         x = int( input( "Enter integer number (-1 to quit) : " ) )
  28.         if x<0:
  29.             break
  30.         fdata.write( str(x)+'\n' )
  31. except IOError:
  32.     print( "Error: can\'t write to file" )
  33. else:
  34.     print( "Written content in the file successfully" )
  35.     fdata.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement