# File: deSpacer1.py # Author: Dr. Gibson # Date: 11/18/2017 # 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) and read in its contents inputFile = open("spaced.txt", "r") contents = inputFile.read() # done with the file, so we can close it now inputFile.close() # use split() to split on (and remove) whitespace characters # use join() to re-create a string with no whitespace chars splitSpace = contents.split() unspacedContents = "".join(splitSpace) spaceLen = len(contents) unspaceLen = len(unspacedContents) spaceChars = spaceLen - unspaceLen # print out total count print("There were", spaceChars, "spacing characters in the file") # write to a new file unspaced.txt (open for writing) outputFile = open("unspaced.txt", "w") outputFile.write(unspacedContents) # close the ouptut file outputFile.close() main()