This lesson convers some of the basics of file input and output using Python, as well as the popular JSON data encoding.
$ echo "hello world" > input.txt
filename = 'input.txt'
f = open(filename)
contents = f.read()
f.close()
print(contents)
-
What is going on here?
- First we create a variable to hold the filename as a string
- Next we pass that variable to the built-in Python
open
function - We read out the contents of the file, and then
close
the file - Finally we print out the contents
-
Simplifying our code
The
close
step in the program is interesting. Almost every fileopen
should be followed by a corresponding fileclose
. Python provides us with a way to make this process slightly easier. The program below is completely functionally equivalent:
filename = 'input.txt'
with open(filename) as f:
contents = f.read()
print(contents)
-
Best practice - use
open
with the code pattern above, with...as. This is called a 'context manager', and will ensure that the file is closed even if your code encounters an exception. -
The colon on line 3 indicates the beginning of a
code block
(also used forif
andelse
conditions). The filef
is only open until we reach the end of the block. -
Let's take a look at the docs for [open][https://docs.python.org/2/library/functions.html#open]
-
So, what modes can we open a file in?
r
- read mode. Read a file from the first byte. Cannot write.w
- delete the whole file and start writing from the first byte.a
- append a file starting at the last byte.rw
- read and write. be careful! if you read up to a point you can overwrite from there. This is challenging to use and you never really need it.b
- the Windows platform considers text and binary files to be different, so we need to specify this if we are writing 'raw' bytes to a file on a Windows computer
Lets try using a file mode:
filename = 'output.txt'
with open(filename, 'w') as f:
f.write('The world is talking back to you.\n')
JSON (JavaScript Object Notation) is a file format which can be used to store and communicate data between computer systems, while still being (arguably) readable by humans. It's commonly used as a format for storing data in files and for as a 'response' format for web APIs.
A JSON document describing some of the metadata about Noisebridge could be written as:
{
"address": "2169 Mission Street, San Francisco, CA",
"name": "Noisebridge",
"open": true,
"members": ["a", "b", "c", ... ]
}
Note that JSON is a little stricter than Python with regards to quotation marks and trailing commas; we always have to use double quotes for fields/values, and we are not allowed trailing commas in lists or objects.
The full JSON format is specified at json.org as a railroad diagram.
Let's use a built-in Python tool to pretty-print some JSON data so that it's more readable for us.
{"url":"www.example.com","metadata":{"crawled_at":"2018-01-15","title":"Example Domain"}}
python -m json.tool example.json`
Python Data Types can be encoded to to JSON (and back!)
This is called encoding
and decoding
. This allows complex data structures to exist easily as flat
strings. It makes it easy to transport all this structure.
Two key actions are encode
and decode
.
In the Python json
module, these will be available as dump/load
and dumps/loads
Encoding and decoding is done by the json module. There are more details here.
>>> import json
>>> help(json)
>>>
>>> my_json_string = u'{"message":"hello world"}'
>>> json.loads(my_json_string)
{u'message': u'hello world'}
>>>
>>> my_dict = json.loads(my_json_string)
>>> my_dict
{u'message': u'hello world'}
>>>
>>> json.dumps(my_dict)
'{"message": "hello world"}'
>>>
"""
import json
data = {"message": "hello world"}
filename = 'data.json'
# write the data into the file data.json
# close the data.json file
# read the data back from data.json
# print the newly-read data and then close data.json