Pages Navigation Menu

"It's not about the destination, it's about the journey. "

Python functions

I’m using this page as a source to store all the Python functions I’ve learned to-date using the “Learning Python the Hard Way” eBook. This is obviously not up-to-date as I’m still continuing to study Python.

open() function

The open function allows you to open files. So far from what I’ve learnt, you use functions in this fashion “variable.function()” (The variable followed by a dot and then the function). So in this case you would type the function in this way to open a file:

Without using argv:

txt = raw_input(“Input the file name you want to read > “)

textfile = open(txt)

print “Here’s your file %s” % txt

print textfile.read()

It should then print the said file within Windows Powershell, using the “read()” function. More detailed information on open() here. read() is also another function and with it Python will not print out the designated file.

seek() function

This function is interesting and is used in exercise 20 of Learn Python The Hard Way eBook.

However, in exercise 20 we are using the argv variable so naturally a file has to be unpacked, and in this case it’s a .txt file.

def rewind(f):
    f.seek(0)

I can’t actually remember what “f” is called but it works in similar ways to a variable; where you can name it anything etc and it can be linked to several equations. Either way, in this exercise seek(0) is essentially inputting a command saying “to print from the first character”, if we changed that value to seek(2) then it would print from the second character. Pretty basic stuff. It seems every other resource I’ve looked into to explain this function explains it in a technical and difficult to understand way. It’s really as simple as I have explained.

Full list of Python functions:

[Source]