# File: moWeSpaced.py # Author: Dr. Gibson # Date: 11/2/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" FILE_IN = "spaced.txt" FILE_OUT = "unspaced.txt" def main(): # open a file for reading (spaced.txt) inFile = open(FILE_IN, "r") # read in the whole thing as one string wholeThing = inFile.read() # count the number of whitespace characters count = 0 unspaced = "" for ch in wholeThing: # if it's a whitespace character, count it if ch == " " or ch == "\t" or ch == "\n": count += 1 # otherwise, append it to a string containing the other characters else: unspaced = unspaced + ch print("count is", count) # create a new file, by opening it for writing (unspaced.txt) outFile = open(FILE_OUT, "w") # write the new stuff to the file outFile.write(unspaced) # close BOTH files inFile.close() outFile.close() main()