Working with Files

From www.norsemathology.org

Jump to: navigation, search

This page will describe how to work with data files from inside of a Sage worksheet.

Contents

Uploading a Data File to Sage

To use an existing data file in Sage the easiest thing to do is upload the file to Sage.

Uploading a file involves several steps:

  • Login to Sage
  • Go into the worksheet you want to use the file with
  • From the data menu at the top select Upload or Create File..
  • You will then be taken to a screen where you can select a file to upload
  • Click the browse button and select the file you want to upload.
    • Optionally you can give the file a new name by entering the new name in the What do you want to call it? text box.
  • Click the Upload File button
  • At the next screen you need to select a worksheet to link the data file to from the drop down list.
    • You can link the data file to as many worksheets as you want.
  • When you are finished linking the data file you can click the worksheet button at the top to go back to the worksheet.
  • You will now notice that if you open the data menu the file you uploaded is listed there.

Now we want to look at how to access the file we uploaded.

Simply put the path to the file can be accessed by DATA+'file_name'

Reading the File

Let's look at a simple example that reads the file line by line and prints out each line.

Let's assume our data file is called results.dat.

fp = open(DATA+'results.dat','r')
for line in fp:
   print line
fp.close()

open is the Sage function for opening a file. The first parameter is the file we want to open, and the second parameter is the access we want. Here we pass r because we just want to read the file. The we can use a for loop over our file pointer fp almost like it was just a list of strings. Each time it loops through the for loop one more line of text is printed out. Then when we're done we close the file.

Writing to a file

Here we will discuss how to create a new file and write some data to it using Sage. The file we will create will be called new_results.txt. We will take a list of integers and write them to the file.

int_list = [1,2,3,4,5,6,7,8,9]
fp = open('new_results.txt','w')
for cur in int_list:
    fp.write(str(cur)+'\n')
fp.close()

When you evaluate the cell containing the line fp = open('new_results.txt','w') Sage will display a link to the file. Once you are finished writing to the file and you have closed the file you can click the link to download the file.

Note: we had to convert each integer to string using str(cur) before writing it to the file. This is necessary for any non string data you want to write to a file.

Writing Files and Storing them in Sage

You can also use the Upload or Create File.. option from the Data menu to create a new file. You can then link the new empty file to your worksheet and write data to it. This would be done in the same way as reading i.e. open(DATA+'file_name','w').

Creating Reusable Sage Functions

Often you may have several Sage functions you wrote that you want to be able to use within other worksheets. This is possible to do with Sage but the process is a bit awkward.

Assuming that your functions are currently inside of a Sage worksheet the steps are as follows:

  • Open the worksheet containing the functions you want in Sage.
  • Open a text editor
  • Copy the functions from Sage and paste them into the text editor
  • Save the file in the text editor as a .sage file in the folder ~/.sage/ (assuming your using Linux)
  • Open a new worksheet in Sage
  • Type load filename.sage (the filename you just saved)
  • Now the functions in filename.sage are accessible in the new worksheet

When using a remote server

By default when using the load command to load a .sage file Sage will look in the ~/.sage/ directory. You can also specify absolute paths such as /home/username/sage/document/file.sage . However you may be using Sage on a remote server where you don't have permission to copy your .sage file to the server. The solution is to upload the .sage file as discussed above.

After you have uploaded your .sage file and linked it to your worksheet you can load it as follows (where filename.sage is the name of your .sage file):

load DATA/filename.sage
Personal tools