brewersfan1976

ch5-ex1.py

Aug 5th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. # this program opens a sample text file
  2. # and capitalizes every word in that file and
  3. # assigns it to a new file
  4.  
  5. # specify the file name to open
  6.  
  7. filename = "sample2.txt"
  8.  
  9. # Open the file in read-only mode
  10.  
  11. file = open(filename, "r")
  12.  
  13. # specify the filename for the new data
  14.  
  15. filename_new = "sample2CAP.txt"
  16.  
  17. # Open the file in write-only mode
  18.  
  19. file_new = open(filename_new, "w")
  20.  
  21. # read entire contents of the input file to capitialize
  22.  
  23. str = file.read()
  24.  
  25. # capitalize every word in the input file
  26.  
  27. cap_words = str.title()
  28.  
  29. # write the data to a new file with every word capitalized
  30.  
  31. file_new.write(cap_words)
  32.  
  33. # we are now done so it is now time to close both of the files
  34.  
  35. file.close()
  36. file_new.close()
Advertisement
Add Comment
Please, Sign In to add comment