# File: tuThSpaced.py # Author: Dr. Gibson # Date: 11/3/2016 # Section: N/A # E-mail: k.gibson@umbc.edu # Description: # This file contains python code that opens a file called "spaced.txt", # counts and removes all of the whitespace, prints out the count, # and saves the result to a file called "unspaced.txt" def main(): # open the file spaced.txt (for reading) cow = open("spaced.txt", "r") # read in the contents of the file contents = cow.read() # count how many whitespace character the file has and print that out count = 0 for ch in contents: if ch == " " or ch == "\n" or ch == "\t": # increment whitespace count count += 1 print("There are", count, "whitespace characters") # remove the whitespace characters (somehow) splitContents = contents.split() joinedContents = "".join(splitContents) # write to a new file unspaced.txt (open for writing) moo = open("unspaced.txt", "w") moo.write(joinedContents) # CLOSE BOTH FILES cow.close() moo.close() main()