Files in Python

Files - Friday Oct 16th and Tuesday Oct 20th



  1. New: Needing Newline Character Bad.pdf is the solved mystery of the Dollar Words program that seemed to create no output.
    Tuesday evening 10/20 at 6:07 pm.

  2. Powerpoint presentation on Python File concepts with Penny Math and Dollar Words example, developed by Sarah Diesburg. Presented in her class on 10/16/Friday and in my class on 10/20/2015.

  3. VIDEO: Friday, Oct 16th 44 minute class: Reading a dictionary file and producing an output file consisting of all the "DOLLAR" words the dictionary contained.
    fin = open("dictionary.txt", "r")    # open file for reading
    fout = open("extracredit.txt", "w")  # open file to save list of DOLLAR words,
                                                   # for extra credit opportunity!
    #For every line, get each word
    for original in fin:                 # get next line of fin, the input file...
        ...
    
        #Write dollar words to file
        if cost==100:
            fout.write(original + "\n")            # write to the output file
    
    print("Output file written called 'extracredit.txt'")    
    
    fin.close()                 # close the files...
    fout.close()
    

  4. Try out the interactive Python Penny Math program.
    Why do we subtract 96 from the ordinal or ASCII value of the char sometimes
          but subtract 64 at other times?
    
    Does either 96 or 64 get subtracted from the ord() of every char?
                                                          -----
    
      65 - 64 is ??     97 - 96 is ??     90 - 64 is ??      122 - 86 is ??
    
  5. Here is the dictionary file. How many dollar words does it contain?
    windsor:~/web/1510/files> wc dictionary.txt   <- wc is Unix word count command.
     80368  80368 622783 dictionary.txt
    
     80368 lines  
     80368 words (so must be one word per line in the file)
    622783 622,783 bytes or characters
                                       in the file dictionary.txt
    
     Question to think about and answer after watching the video
    and studying the programs:
    
    622783  / 80368
    7.74914144933306788771
                                   622783 
                                  --------  =  7.74914144933306788771
                                    80368
    
        Is the average word length of words in dictionary.txt = 7.75
    
    

  6. Here is the Python files dollar words program developed in the video during Friday's class. It assumes you have an understanding of the interactive interactive Python Penny Math program.

    Compare it to the original version available to Sarah's class a day or two before Friday's class. They should be pretty similar, but any different approach that you notice and understand will only increase your fluency with Python and programming in general!

    Try it out. First download the dictionary.txt file and save it in the same directory where you will save and run your dollarWords.py program.
    Look at the extracredit.txt file to see the output when you are done with the run of dollarWords.py. The file extracredit.txt will be just those words from dictionary.txt that are DOLLAR words!

    Output file written called 'extracredit.txt'
    
    There were 1018 dollar words out of 80368 dictionary words.
    >>>        ----                     -----
    
  7. Here is the dollar words screen output from a more developed version of Dollar Words. I added a few features to it on UNI homecoming eve eve and eve.

    The more developed version with additional features: dollarWords3.py ...

  8. Annotated handout of dollarWord3.pdf with some of the output file.