How to Open Files in Python 3


show more Opening files provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Opening files

Python has some very simple built- in functions for dealing with files. Let's make a working copy of files.py. I'll call it files-working.py and we will open that working file and we see here a simple loop that reads lines from a file and prints them out. So run that and see what it does. It prints out the lines of this file. This is the file here, lines.txt, and we are just printing out all five lines with that file in this little loop. What we are going to look at in this movie is this open function.

It's very simple and it has a few options. By default, all it does is it opens the files in the read mode and you give it a file name here and it returns the file handle object, which can then be used to operate on the file. If you don't specify it, read mode is the default and that looks like this. There is the second argument, which is the mode, and that defaults to the r character for read. Options there are r for read, w for write, or a for append.

Append is a special write...

Opening files
Video duration: 2m 4s 6h 36m Beginner

Viewers:

Opening files provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...