Working with files in Python is much like working with files in other scripting languages. Python provides the standard Unix open modes through its open() function.
- Python provides the open function for opening files.…Here in Komodo, I've opened a working copy of files.py…from chapter 12 of the exercise files.…And in a nut shell, we open the file and we read the file…line by line, stripping the new lines off the end…of each line, and display them.…So when I run this, you see, we have this output here.…I'm just going to open this up a little more so we can see…the whole file, first line through tenth line.…And this is the file here, you notice the file is lines.txt.…
And, I have the original file here, and you can see…that we're taking each of those lines and we're displaying…them faithfully.…So let's take a little bit of a look at how this works.…The open function returns a file object.…It takes a file name and opens that file and returns…a file object.…The file object itself is an iterator, and so we can…use a four loop and get one line at a time…without having to buffer the entire file and memory.…
And there's a method here you may not have seen…on the string, obviously each of the lines is returned…
- 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 Efficiently (2017)with 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.