From the course: Learning Python

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Writing files

Writing files

- [Narrator] Python provides built-in methods for working with files and directories. You can open files, write data into them, read the data back in, get information about the files and so on. And that's what we're going to take a look at in this chapter. So let's begin by opening up the writefile_start file here in the editor. If you're using Colab, just go ahead and make a new code cell. Now I don't need to import any modules to work with files because the functions for working with files are just built into the base Python language. So for our first example, let's just write some information to a text file. So the way that I do this is I'm going to create a new variable named sample_file, and then I'm going to call the open function. The open function takes a couple of parameters. The first is the name of the file to operate on, so I'll call that textfile.txt. And then there's the kind of access to the file that you want. So I'm going to open the file for write access. And so I'm…

Contents