A text file is typically read and written line-by line. This video demonstrates how to open and copy a text file.
- [Instructor] Reading and writing text files…is remarkably easy in Python.…Here in Komodo, I've opened a working copy of copy-text.py…from Chapter 12 of the exercise files.…Here we have our input file which is 10 lines of text,…and it's called lines.text, and so when I…use the open function, I'm opening lines.text.…I'm opening it in read mode and in text mode,…and of course this is the default.…
If I were to simply omit this argument,…it would still open in read mode and text mode,…but I like to be explicit, and remember the Python…principle that explicit is always better than implicit.…In the next line, I'm opening the output file,…and this is called lines-copy.text,…and you notice in our file listing over here on the left,…there's lines.text, but we do not have…a lines-copy.text yet.…That will be created when I open this file in write mode,…and again I'm specifying the t for text mode…even though it is the default.…
Now our infile is the one that we open for reading,…and so I'm using a for loop to read the file,…
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.