Opening a file

In this subsection, you will perform the following:

  1. Create and organize a project folder for data wrangling.
  2. Place your data in the project folder.
  3. Write a program to open and close a data file.

In later sections, you will build on this program to read and process the data. The following steps will guide you through the process of setting up your file system and opening the data file.

  1. Open Atom. If you have another text editor that you prefer, you may use that one. Create a project folder called ch3 and add the project folder to the file tree. Inside the ch3 folder, create a file called process_data.py. Additionally, inside the ch3 folder, add an additional folder called data:
  1. If you haven't already, download the scf_data.json file from the data folder of the reference material and place the data in your data folder:
  1. In your process_data.py file, use the open() function to open scf_data.JSON with read ('r') permission:
inFile = open("data/scf_data.json",'r')

Note that if you are using Windows, there is an important difference to keep in mind when writing relative path strings in Python. Windows uses the backslash character to separate folder names in a file path, while macOS and Linux use the forward slash / character. However, a backslash character in Python is used for another purpose, so in Windows you will need to write two backslashes \ in order to separate folders. The following is the Windows version:

inFile = open("data\scf_data.json",'r')

I will be using the macOS/Linux version to create file paths in future programs, so be sure to change this if you are using Windows.

  1. It is good practice to explicitly close the file when you are done using it. You can do this using the file object's close() function:
inFile = open("data/scf_data.json",'r')
inFile.close()
  1. Next, I'm going to jump ahead a bit, just to prove that the previous example does something useful. In the following continuation of process_data.py, you will use the read() function of the file object to read and print the raw contents of the data file:
inFile = open("data/scf_data.json",'r')
print(inFile.read())
inFile.close()
  1. Go ahead and run the previous file. You should see the raw data from the file printed to the output:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset