Advertisement
brilliant_moves

MakeFile.py

Feb 6th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. #!/usr/bin/python
  2. #MakeFile.py
  3. #Python 2.7
  4. #Make a new text file
  5. #Chris Clarke
  6. #13.08.2014
  7.  
  8. import sys
  9.  
  10. filename = raw_input("Enter name of file to create: ")
  11.  
  12. try:
  13.    fn = open(filename, "r") #see if file exists
  14.    print "File exists. Choose another name." #don't over-write files!
  15.    fn.close()
  16.    sys.exit(1)
  17. except IOError:
  18.    pass
  19.  
  20. try:
  21.    fh = open(filename, "w")
  22.    all_text = ""
  23.    sent = raw_input ("What to use for end of input (e.g. 'zzz'): ")
  24.    print "Now enter text. To quit, enter", sent, "on a new line. Begin typing!"
  25.    while True:
  26.       line = raw_input()
  27.       if line == sent: break
  28.       all_text += line+"\n"
  29.    fh.write(all_text)
  30. except IOError:
  31.    print "Error: can't create file!"
  32. else:
  33.    print "Written content in the file {} successfully.".format(filename)
  34.    fh.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement