Follow me on Twitter RSS FEED

Python Tutorial 4: File IO

Posted in

If you have worked with PHP or Perl, then file IO should be familiar. To edit/read a file, you need a file object. To get the file object, you have to open the file.

fileObject = open("file.ext", "r")
print fileObject.read()

But what if you wanted to start reading from the middle of the file? In python, this is possible. To set the file read cursor, you must use the seek function:

fileObject.seek(offset,where)

where is optional, and it tells the compiler where to start from in the file. If where is 0, then it puts the cursor at offset characters from the start of the file. If it is 1, the characters are counted form where the cursor is currently. If 2, then they are counted from the end of the file. If where is not specified, then the compiler uses 0.

The offset [as previously explained] tells the compiler how far from where to set the cursor. Negative numbers are acceptable

  • fileObject.seek(15,0) would move the cursor 15 characters from the beginning of the file
  • fileObject.seek(15,1) would move the cursor 15 characters to the right from its current position
  • fileObject.seek(-15,2) would move the cursor 15 characters away from the end of the file

Notice the negative sign before the fifteen in the last example.

seek, read, and open are the most basic IO functions. Lets look at some more: tell(), readline(), readlines(), write() and close()

tell() gets the position of the cursor in the file, which [as shown above] can be set with seek().

cursorPos = fileObject.tell()

The readline() function reads from the cursor position to the end of the line. The readlines() function reads from the cursor position to the end of the file, and returns an array with each line in a separate index. You will never guess what the write() function does. Ok, you might if you are really smart. Assuming you arn't really smart, I'll tell you what it does. It writes to the file. It writes at the cursor position, overwriting any characters infront of it, like if you press the Insert button in Microsoft Word and type. The close() function closes the file object so you can't read or write to it anymore without reopening the file. You should always remember to do this because if you don't, then the changes you made most likely won't effect the file.

Now lets talk about pickles. No I'm not being random; Pickles are a way in python to save objects to files. You can save almost any object to a file with pickles. Inorder to use them, though, you have to import them with the statement: import pickle. Then you use the pickle.dump() function to save them to the file:

import pickle
numbers = ["one",2,"three","four",5,"can you count?"]
fileObject = open("numbers.txt", "w")
pickle.dump(numbers,fileObject)
fileObject.close()

To read a pickle back in, you use the pickle.load() function:

import pickle
fileObject = open("numbers.txt", "r")
numbers = pickle.load(fileObject)
fileObject.close()
for item in numbers
    print item

The main con with pickles is that you can't save more than one object to a file. A way around this would be to put all the objects into an array, and then save the array to the file.

That concludes my fourth python tutorial.

0 comments:

Post a Comment