2 Call the read or write method on the object 3 Close the file by calling the

2 call the read or write method on the object 3 close

This preview shows page 27 - 34 out of 34 pages.

2. Call the read() or write() method on the object. 3. Close the file by calling the close() method on the object. FILE READING AND WRITING
Image of page 27
The open(filename, mode) is used to create a text object that contains the text of a text file. The mode can either be 'r' for read, 'w' for write or 'a' for append. If not specified, it defaults to read. The read() method is used to read the entire file's contents into a string. The readlines(method) is used create a list that contains one string element for each line of text in the file. The write() method will either overwrite the file with new content or append to the end of the file depending on whether the file was opened in the write or append modes. The close(method) updates (if necessary) and closes the text file. FILE READING AND WRITING METHODS
Image of page 28
Assume that you have a text file named 'languages.txt' that contains the following text: OPENING AND READING TEXT FILES newfile = open('e:\\python\\languages.txt', 'r') newfiletext = newfile.read() print(newfiletext) results in 1. Java 2. Python 3. Basic 4. Perl 5. C++ 1. Java 2. Python 3. Basic 4. Perl 5. C++
Image of page 29
Assume that you have a text file named 'languages.txt' that contains the following text: OPENING AND WRITING TEXT FILES newfile = open('e:\\python\\languages.txt', 'a') newfile.write('\n6. Pascal\n7. Fortran') newfile.close() newfile = open('e:\\python\\languages.txt', 'r') newfiletext = newfile.read() print(newfiletext) results in 1. Java 2. Python 3. Basic 4. Perl 5. C++ 1. Java 2. Python 3. Basic 4. Perl 5. C++ 6. Pascal 7. Fortran
Image of page 30
The Python shelve module is used to store python variables to disk. It can be used to store any type of Python variable. The shelve object is used the same as a dictionary with a key and a value assigned. The variables are stored in binary files and can be opened when needed. USING THE SHELVE MODULE
Image of page 31
Assume that you have created a game and at the end, you wish to save the following Python variables SAVING VARIABLES TO A SHELVE FILE import shelve gamedata = shelve.open('datafile') gamedata['score'] = score gamedata['highscores'] = highscore gamedata['levelscores'] = levelscores gamedata['attributes'] = attributes gamedata.close() score = 125000 highscore = 257687800 levelscores = [2500, 4700, 7850, 6540, 9870] attributes = {"strength":80, "speed":75, "agility":90} You could save them using the shelve module as follows:
Image of page 32
Assume that you have created a game and at the beginning, you wish to restore the following Python variables RESTORING VARIABLES FROM A SHELVE FILE import shelve gamedata = shelve.open('datafile') score = gamedata['score'] highscores = gamedata['highscores'] levelscores = gamedata['levelscores'] attributes = gamedata['attributes'] gamedata.close() score highscore levelscores attributes You could restore them using the shelve module as follows: print(score) print(highscore) print(levelscores) print(attributes) results in 125000 257687800 [2500, 4700, 7850, 6540, 9870] {'strength': 80, 'speed': 75, 'agility': 90}
Image of page 33
THE END
Image of page 34

You've reached the end of your free preview.

Want to read all 34 pages?

  • Spring '19
  • Filename, File Paths

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors