Writing To Text Files

1. Introduction

The most common type of files we'll be dealing with is "table-based" data similar to what you've used in Microsoft Excel or another spreadsheet package. This data is typically stored with each row in a "line" in the file. Each cell is then stored in the line and separate or "delimited" by a special character.

2. Writing a text file

Python makes it very easy to write to a text file. Try the following code and then open the file in a text editor.

Note: You may receive an error message that you do not have permission to write to a folder. Modern computers block your ability to write to certain folders, including the root folder, to protect against certain viruses. A good approach is to create a "Temp" folder for these exercises.


Note: The quotes above have been "fixed" to be regular quotes that are organized by Python. Sometimes you'll paste code like that above in a Python environment and receive "syntax errors". This is because HTML uses special quotes that point left and right around text. These will need to be replaced by regular double quotes which are the ones available on your keyboard.

The first parameter in the "open()" function is a path to a text file. This can be a new string or an existing variable with a string in it. The second parameter indicates whether you want to "write" to the file or "read" from it. "w" indicates that we want to write to the file and delete and existing file if it is already there.

Now try the following code which has added a "new line" or "n" character to the end of each line.

TheFile.write("Row 1\n") 
TheFile.write("Row 2\n") 
TheFile.write("Row 3\n") 

Open the file in a word processor and you would see that you have 3 rows of text. You can know create your own text files for a wide variety of applications.

Note: Windows and UNIX based operating systems use different characters to terminate the end of a line in a text file! Windows uses a carriage return and a line feed character (CR + LF) while UNIX uses just a linefeed character (LF). The Mac OS before version X used just a carriage return (CR). You'll see this when you load a file into a text editor and the lines do not match up as they should. The way to get around this is to always write out the correct sequence of characters for the operating system you are working on but then treat any combination of CR and LF as a line ending when reading files.

3. File Paths

In the example above, you may have noticed that the "slash" that is used is a "forward slash" instead of a "backslash". This because the "backslash" is used for special characters os if we used "\test.txt", Python would convert the "\t" to a tab character. To avoid this you can either use forward slashes ("/") or two backslashes ("\\").

Do not use any characters in file path except for text, numbers, and underscores. While the path might work on one computer, it may not work on another or with a different software package.

Additional Resources

Python Documentation: Files

© Copyright 2018 HSU - All rights reserved.