5.3. File Input / Output#

When you have to type your messages directly into your Notebook, you probably will limit yourself to very short messages since it would take a long time to type longer passages of text by hand. In this section we’ll see how Python can import text from other files.

Importing Text#

If you have your text you wish to import saved as a .txt file, you can have Python read that file and save it as a string. To make things easy your text file should be in the same folder as your notebook file or in a subfolder. The syntax to import text into your notebook is:

with open('yourfile.txt') as f:
    myText = f.read()

print(myText)
Alice was beginning to get very tired of sitting by her sister on the
bank, and of having nothing to do: once or twice she had peeped into the
book her sister was reading, but it had no pictures or conversations in
it, 'and what is the use of a book,' thought Alice 'without pictures or
conversations?'

You can tell from the code and matching output that my file was named yourfile.txt and it contained the text of the first paragraph of the first chapter of Alice in Wonderland. (You can find a lot of text that is free and publicly available at Project Gutenberg). I stored this string to the variable myText. All file and variable names can be changed to whatever you like depending on what your text file is named and what you want to store it as.

Now that you have your text imported as a string, you can perform all the usual operations on it.

print( text_clean( myText ) )
ALICEWASBEGINNINGTOGETVERYTIREDOFSITTINGBYHERSISTERONTHEBANKANDOFHAVINGNOTHINGTODOONCEORTWICESHEHADPEEPEDINTOTHEBOOKHERSISTERWASREADINGBUTITHADNOPICTURESORCONVERSATIONSINITANDWHATISTHEUSEOFABOOKTHOUGHTALICEWITHOUTPICTURESORCONVERSATIONS
print( caesar( myText, 17 ) )
RCZTVNRJSVXZEEZEXKFXVKMVIPKZIVUFWJZKKZEXSPYVIJZJKVIFEKYVSREBREUFWYRMZEXEFKYZEXKFUFFETVFIKNZTVJYVYRUGVVGVUZEKFKYVSFFBYVIJZJKVINRJIVRUZEXSLKZKYRUEFGZTKLIVJFITFEMVIJRKZFEJZEZKREUNYRKZJKYVLJVFWRSFFBKYFLXYKRCZTVNZKYFLKGZTKLIVJFITFEMVIJRKZFEJ

Saving Text to File#

Since reading long lines of text can be difficult in a Jupyter Notebook, you may wish to save the contents of a variable back to a text file to read in a different application. To do so, use the following syntax:

with open('output.txt', 'w') as f:
  print(myText, file=f, end='')

This code took the contents of the variable myText and stored it to the file output.txt in the same folder of the notebook. The file output.txt will be created if it didn’t already exist. If it did already exist, it will be overwritten. Now you can download this file and open it on your computer in any application that you’d like.

More Details on the open() Function#

The open() function has a few more options that may be helpful to you.

with open('filename.txt', 'option') as f:...

You can replace 'option' with the following characters to denote how you wish to interact with the file you’re opening.

character

meaning

'r'

Open for reading only (default if no character specified)

'w'

Open for writing

'a'

Open for appending. Will add to the end of the file instead of overwrite

'x'

Create this file. Will not work if the file already exists

't'

Text mode (default)

'b'

Binary mode

You can combine characters when specifying how to open a file. For example:

with open('filename.dat', 'wb') as f:...

Would open the file filename.dat to write binary to it.