Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Can anyone explain the 'open' function?

Ive clicked on the link to python documentation, but i don't understand a single word of it, and it doesn't explain how to use it at all

2 Answers

open can read any file, but you will probably find the most useful one

with open(filename, "r") as file_object:
    file_content = file_object.read()

print(file_content)

the "r" stands for "read", just like you can do "w" for "write", in which case it looks like this

with open(filename, "w") as file_object:
    file_object.write("your text here")

The reason you want to use a with block is because it automatically closes the file. (you can read more about this in the documentation)

My tip for you is to learn to read documentation. You are going to need it at some point.

Best of luck

cheers man, yeah im deffo going to have to get used to it, just looked like garbage first time i looked. Thanks for the advice, you too Steven, between these and a good study of the docs im sure i can get there.

Steven Parker
Steven Parker
243,318 Points

At its simplest, open just gets ready for reading from a file. You pass it the name of the file and it returns a "file object" that you will use later to actually read from the file. For example:

thefile = open("myfile.txt")

Then later, if you wanted to print one line from the file, you might do this:

print(thefile.readline())

If you want to do more to the file than just read from it, you will pass an additional argument to open to show what you are going to do. But for basic reading, this is all you need.

sorry i still dont get it at all, add an argument? and so it doesn't let me use whats on the other page just read a line?

ive got 2 files, the main script game2.py and a file called data.py with some lists and variables etc.

at the moment, in the main script ive done 'import data' which i read is how to use more than one page. then all like 'words = data.words' and 'intro = data.intro' so that i can use the stuff from the other page. seems to work but the teacher said to use 'open' and 'read'. how would i use open and read in that sort of way? or is it not used for that? im well confused.

also, how did you just post those little pictures of code you have written?

Hi Steven, Thanks for your reply. My doubt is where to SAVE myfile.txt in order for python to be able to open it? Do I have upload it somewhere? Thank you, Aquiba