From the course: Python Standard Library Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Basic file operations - Python Tutorial
From the course: Python Standard Library Essential Training
Basic file operations
- In this chapter, we're going to look at various ways of working with file information, and Python provides built in support for working with files, just like most other programming languages. So, I don't need to import any special modules just to do some basic work, which is what we're going to focus on in our first example. So, let's open up "filesbasic_start", and for the first example, we'll just create a file and write some data into it. So, to open a file, you just need to use the open function along with a string argument that specifies the type of access that you need. So, I'm going to write data to the file, so I'm going to write, "fp = open", and I'll name my file, "testfile.txt". So, I want to write data to my file, so I'm going to use the "w" character to indicate that I want to write. So, the open function returns a file handle that refers to that file. The right function then lets us actually write the data to the file, and since I used a "w" to indicate the file mode…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.