baconfile close(baconfile open('bacon txt'content baconfile read(baconfile close(print(contenthelloworldbacon is not vegetable firstwe open bacon txt in write mode since there isn' bacon txt yetpython creates one calling write(on the opened file and passing write(the string argument 'helloworld/nwrites the string to the file and returns the number of characters writtenincluding the newline then we close the file to add text to the existing contents of the file instead of replacing the string we just wrotewe open the file in append mode we write 'bacon is not vegetable to the file and close it finallyto print the file contents to the screenwe open the file in its default read modecall read()store the resulting file object in contentclose the fileand print content note that the write(method does not automatically add newline character to the end of the string like the print(function does you will have to add this character yourself as of python you can also pass path object to the open(function instead of string for the filename saving variables with the shelve module you can save variables in your python programs to binary shelf files using the shelve module this wayyour program can restore data to variables from the hard drive the shelve module will let you add save and open features to your program for exampleif you ran program and entered some configuration settingsyou could save those settings to shelf file and then have the program load them the next time it is run enter the following into the interactive shellimport shelve shelffile shelve open('mydata'cats ['zophie''pooka''simon'shelffile['cats'cats shelffile close(to read and write data using the shelve moduleyou first import shelve call shelve open(and pass it filenameand then store the returned shelf value in variable you can make changes to the shelf value as if it were dictionary when you're donecall close(on the shelf value hereour shelf value is stored in shelffile we create list cats and write shelffile['cats'cats to store the list in shelffile as value associated with the key 'cats(like in dictionarythen we call close(on shelffile note that as of python reading and writing files