0
Posted January 9, 2013 by Hunter in Python Programming
 
 

Basic Text File Management in Python

free-the-files-470x340
free-the-files-470x340

Everything uses files.  You want to store data between uses of a program?  Create and store in a file, and then open and read from that file next time the file is run.  Have a huge database that changes daily, and you need to manipulate data from the database?  You’ll need to open and read from the file where the data is stored.

This is all very easy in Python.  In this tutorial we’re going to learn how to

Create
Open
Write
Read
Position
Close
Rename
Delete

a simple text file.

Create and Open

Python tries to be as simply and intuitive as possible, and towards that end the developers have written the language such that o create a plain text file in Python, you only need to try to open one that isn’t there.

f = open('filepath', 'access')

I will create one on my desktop.

f = open('/Users/myusername/Desktop/newfile', 'w+')

The access argument of the open function determines, practically, the readability and writability of the file.  A complete list of access modes is given below.

Access Mode Means…
r Reading only.  Pointer is at the beginning of the file.  Will not create a new file if file does not exist.
rb Reading only, in binary.  Pointer is at the beginning.  Will not create a new file if file does not exist.
r+ Reading and writing.  Pointer at the beginning.  Will not create a new file if file does not exist.
rb+ Reading and writing, in binary.  Pointer at the beginning.  Will not create a new file if file does not exist.
w Writing only.  Overwrites existing file.
wb Writing only, in binary.  Overwrites exisiting file.
w+ Writing and reading.  Overwrites exisiting file.
wb+ Writing and reading, in binary.  Overwrites exisiting file.
a Appending.  Pointer at the end of the file, can write to file.
ab Appending in binary.  Pointer at the end of the file, can write to file.
a+ Appending and reading.  Pointer at the end of the file.  Can write to and read from file.
ab+ Appending and reading, in binary.  Pointer at the end of file.  Can write to and read from file.

You see from this list that you cannot create a new file if it is opened in any of the ‘read’ modes.  That’s fine, though, because if you do not know if a file has been created or not, and need to access it (without overwriting it) if it exists or create it if it does not, then you can just ‘open’ the file in a+ mode.

So now I have a file called newfile on my desktop that I can both write to and read from, and a file object called f.  I would modify f to modify the file, newfile.

Write

The basic writing method is

f.write(string)

This will write the string string to the file at the current position.

For my file, newfile, since I just created it in the w+ mode, there is nothing in the file and therefore only one position.  So let’s write something to the file.

name = raw_input("What is your name? ")

quest = raw_input("What is your quest? ")

color = raw_input("What is your favorite color? ")

f.write("Name: "+name+"\nQuest: "+quest+"\nColor: "+color)

Now let’s read what I’ve written!

Read

The basic reading method is

f.read(size)

This will return the first size bytes from the file.  So, to return one character,

f.read(1)

If you wanted to read the file all the way to the end, simply leave size blank.

f.read()

But you may want to read a line at a time, but you don’t know how long the line is.  In that case, you can use readline().

f.readline()

This will return the rest of the line on which the pointer currently is.

So let’s read what my file says.  f.read() gives the output:

Nothing!  Absolutetly nothing!

That’s because the pointer was at the end of the file.

Position

You will want to both know the pointer’s position and move the pointer.  The method

tell()

returns the pointer’s position, in bytes from the start.  This may or may not be helpful, but will convey greater information if you knew the total file size.  One way of learning the number of bytes in the file is to read() the file to the end, and then call tell().  This method can also be used to determine the size of a line, by calling tell() at the beginning of a line, then readline(), then tell() again, and mathing your way to the size of the line.  Then, to move the pointer back to the desired position, use

seek(byte)

which moves the pointer to the specified byte within the file.  seek(0) moves the pointer to the beginning.

I’m want to be sure that my name, quest, and favorite color were correctly written to newfile, so I’m going to read it.

f.seek(0)

print f.read()

Output:

Name: Hunter\nQuest: To seek the Holy Grail\nColor: Blue

[Output may vary.  You may see a new line instead of the literal '\n'.  Open up the file itself to see that new lines exist where they should.]

But as we know, what really matters is the final question.  So that’s the only line I want to print from this file.  So I know I want to print the last line, but how do I know how many lines there are?  Well, I’m going to use

REGULAR EXPRESSIONS!

because they’re fun.

# I haven't imported this module yet, so I'll need to to use the findall method

import re

# I had previously read() the file, so I need to put the pointer back at the beginning

f.seek(0)

# fulltext will contain all of what is in the file

fulltext = f.read()

# This searches for all newline characters in the string fulltext, and returns them in a variable, match, in a list.

match = re.findall(r'\n',fulltext)

# There will be a newline character after every line except the last one, so there are one more lines than there are newline characters.

lines = len(match) + 1

print "There are " + str(lines) + " in this file."

Output:

There are 3 lines in this file.

So, knowing how many lines there are, I can now read only the last line:

f.seek(0)

for line in range(lines-1):
  f.readline()
  print f.readline()

Output:

Color: Blue

Close

Alright, I’m done.  I’ve passed the bridge, and I don’t have any more questions to record or read at the moment.  I’m going to close the file with the close() method.

f.close()

Done.

Rename

Now that I know what this file is about, I’m going to give it a more appropreate name.  I’m going to use the rename function,

os.rename(current_name, new_name)

The file contains the answers to three questions, so I’m going to call it ‘QUESTions’.  This requires the os module, so I’m going to import that first.

import os

os.rename('/Users/myusername/Desktop/newfile', '/Users/myusername/Desktop/QUESTions')

Delete

The fields lose the crack of the bat, the rumbles and grumbles of engiens fade from the streets, doors close and guests leave, lights flick off and the sun slips from his precarious perch on the edge of the world, and all things come to and end.  When this time comes for your file, you will want to know how to delete it.  And with simiplicity equal to the fundamentality of trancience in this world, I submit the remove() method.

os.remove('/Users/myusername/Desktop/QUESTions')

Hunter