Files
Learn how to work with files in an automated way! Investigate the properties of text, CSV, and JSON files by reading and writing to them!
StartKey Concepts
Review core concepts you need to learn to master this subject
Python File Object
Python Readline Method
Parsing JSON files to dictionary
Python Append To File
Python Write To File
Python Readlines Method
Class csv.DictWriter
Python Read Method
Python File Object
Python File Object
A Python file object is created when a file is opened with the open()
function. You can associate this file object with a variable when you open a file using the with
and as
keywords. For example:
with open('somefile.txt') as file_object:
You can then print the content of the file object, file_object
with print()
.
print(file_object)
You might see something like this on the output terminal:
<_io.TextIOWrapper name='somefile.txt' mode='r' encoding='UTF-8'>
- 1Computers use file systems to store and retrieve data. Each file is an individual container of related information. If you’ve ever saved a document, downloaded a song, or even sent an email you’ve …
- 2When we read a file, we might want to grab the whole document in a single string, like .read() would return. But what if we wanted to store each line in a variable? We can use the .readlines() func…
- 3Sometimes you don’t want to iterate through a whole file. For that, there’s a different file method, .readline(), which will only read a single line at a time. If the entire document is read line b…
- 4Reading a file is all well and good, but what if we want to create a file of our own? With Python we can do just that. It turns out that our open() function that we’re using to open a file to read …
- 5So maybe completely deleting and overwriting existing files is something that bothers you. Isn’t there a way to just add a line to a file without completely deleting it? Of course there is! Instead…
- 6We’ve been opening these files with this with block so far, but it seems a little weird that we can only use our file variable in the indented block. Why is that? The with keyword invokes something…
- 7Text files aren’t the only thing that Python can read, but they’re the only thing that we don’t need any additional parsing library to understand. CSV files are an example of a text file that impos…
- 8Recall our CSV file from our last exercise: users.csv Name,Username,Email Roger Smith,rsmith,[email protected] Michelle Beck,mlbeck,[email protected] Ashley Barker,a_bark_x,[email protected]…
- 9I need to level with you, I’ve been lying to you for the past two exercises. Well, kind of. We’ve been acting like CSV files are Comma-Separated Values files. It’s true that CSV stands for that, bu…
- 10Naturally if we have the ability to read different CSV files we might want to be able to programmatically create CSV files that save output and data that someone could load into their spreadsheet s…
- 11CSV isn’t the only file format that Python has a built-in library for. We can also use Python’s file tools to read and write JSON. JSON, an abbreviation of JavaScript Object Notation, is a file for…
- 12Naturally we can use the json library to translate Python objects to JSON as well. This is especially useful in instances where you’re using a Python library to serve web pages, you would also be a…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory