Lesson goal: Introduction to data files

Previous: Make an (x,y) plot | Home | Next: Reading data from a file

With the Internet, mobile phones, and sensors, we live in the age of information, or "data." Data science encompasses the idea that decisions and discoveries can be made by mining (or looking deeply into) into all of this data. In this lesson (and the ones that follow), we'll show you how to mine into data using your coding skills.

If you are working in data science, you must have access to the data somehow. Often it'll be stored in a database, or in a data file that you can access. This lesson will introduce you to the idea of a data file, and how to access it, in order to look at the numbers (or "data"), it contains. We'll try to discover some things about the data along the way.

To start, a data file on a computer has a name. Here we'll use a file called "sample.dat." To begin using the file, you have to "open it first." Here, data files are open for use using the file_open function.

handle=file_open("filename")
Move the mouse over a dotted box for more information.

When you open a file using file_open, the computer will go out and see if the file exists, and that you have permission to use it. If all is OK, file_open will return a valid file handle. A valid file handle will contain a number like 0, 1, 2, 3... If the file is not available, file_open will return a -1, as an "error" condition.

Now you try. Run the code and see if the file sample.dat can be opened.

Type your code here:


See your results here:

This code will not run! Put in appropriate print statements in the if statement to tell the programmer if the file can be opened or not. Remember: What does it mean if file_open returns a -1 into the handle variable?

If you get sample.dat to open OK, try misspelling its name with something like sample.dax or sapmle.dat or sample.datt and see what happens. Dismiss.

Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.