#---------------------------------------------------------------------- # Illustrating reading from and writing to files. # See pages 264-265 in the Companion # # Copyright (C) September 25, 2017 -- Dr. William T. Verts # # NOTE: In all the examples below, the first statement in each function # (Filename = pickAFolder() + "Data.txt") uses a built-in function from # JES that is not present in pure, traditional Python. The pickAFolder # function gives you a nice dialog box so you can select a folder # without knowing the text-based path to that folder. If you happen to # know the path, the statement Filename = raw_input("Enter a folder") # will allow you to type it in directly, using only the functions from # traditional Python. For now, pickAFolder is far more convenient. #---------------------------------------------------------------------- #---------------------------------------------------------------------- # Write four strings to Data.txt, but all on one line. #---------------------------------------------------------------------- def Main1(): Filename = pickAFolder() + "Data.txt" print Filename MyFile = open(Filename, "w") MyFile.write("Hello") MyFile.write("World") MyFile.write("Monday") MyFile.write("afternoon") MyFile.close() return #---------------------------------------------------------------------- # Write four strings to Data.txt, but one per line. The \n escape code # indicates a "new line" to be written into the file. Python # programmers are responsible for making sure that the text they write # to files contains the appropriate line breaks. See the table on page # 250 of the Companion for all available escape codes. #---------------------------------------------------------------------- def Main2(): Filename = pickAFolder() + "Data.txt" print Filename MyFile = open(Filename, "w") MyFile.write("Hello\n") MyFile.write("World\n") MyFile.write("Monday\n") MyFile.write("afternoon\n") MyFile.close() return #---------------------------------------------------------------------- # Write three strings to Data.txt, one per line, where the first line # contains a literal backslash (the first line will be Hello\World). #---------------------------------------------------------------------- def Main3(): Filename = pickAFolder() + "Data.txt" print Filename MyFile = open(Filename, "w") MyFile.write("Hello\\") MyFile.write("World\n") MyFile.write("Monday\n") MyFile.write("afternoon\n") MyFile.close() return #---------------------------------------------------------------------- # Write 10000 lines to Data.txt, each containing the number of the # line and its square root. Notice that the item passed to write # is a single string composed of four pieces: (1) the int I converted # to a string, (2) the literal string with the equal sign and a blank # on each side, (3) the float returned by sqrt converted to a string, # and (4) the new line. #---------------------------------------------------------------------- def Main4(): Filename = pickAFolder() + "Data.txt" print Filename MyFile = open(Filename, "w") for I in range(10000): MyFile.write(str(I) + " = " + str(sqrt(I)) + "\n") MyFile.close() return #---------------------------------------------------------------------- # Read the contents of Data.txt into a single, long string X (including # all the new line characters). #---------------------------------------------------------------------- def ReadFromFile1(): Filename = pickAFolder() + "Data.txt" print Filename MyFile = open(Filename, "r") X = MyFile.read() # read() grabs file as one long string MyFile.close() print X return #---------------------------------------------------------------------- # Read the contents of Data.txt into a list X, one line from the file # in each entry of the list (including all the new line characters). #---------------------------------------------------------------------- def ReadFromFile2(): Filename = pickAFolder() + "Data.txt" print Filename MyFile = open(Filename, "r") X = MyFile.readlines() # readlines() grabs file one line per list entry MyFile.close() print X return #---------------------------------------------------------------------- # Read the contents of Data.txt into a list X, one line from the file # in each entry of the list, but then use a list comprehension to strip # the new line characters off the right end of each string (using the # rstrip function). This is likely to be the most commonly used form. #---------------------------------------------------------------------- def ReadFromFile3(): Filename = pickAFolder() + "Data.txt" print Filename MyFile = open(Filename, "r") X = MyFile.readlines() MyFile.close() X = [C.rstrip("\n") for C in X] # List comprehension to strip new lines print X return