# File: deSpacer1.py # Author: Dr. Gibson # Date: 4/18/2018 # 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 = "/afs/umbc.edu/users/k/k/k38/pub/cs201/spaced.txt" def main(): # open a file for reading (spaced.txt) ifp = open(FILE, "r") # read in the whole thing as one string text = ifp.read() ifp.close() # count the spacing characters without = "" count = 0 for i in range(len(text)): # if it's a spacing character, count it if text[i] == " " or text[i] == "\t" or text[i] == "\n": count += 1 # otherwise, save the character to the 'without' string else: without += text[i] print("There were", count, "whitespace characters in", FILE) # save to a new file without any whitespace ofp = open("unspaced.txt", "w") ofp.write(without) ofp.close() main()