Silly, fun and useful things to do with Resolver One
  • HOME
  • About
  • Articles
  • Fun
  • Snippets
  • Disclaimer
  • Useful Links

Site by Voidspace

Email Michael

Download Resolver One

RSS Feed RSS Feed

Resolver Hacks contains information and code for doing fun and useful things with Resolver One. This site is created and maintained by Michael Foord, not by Resolver Systems. Please read the disclaimer before using any of the code on this site.

Site Built with rest2web

Creative Commons License

IronPython Wiki

IronPython in Action IronPython in Action

Accessing Files with __file__

It is common for a spreadsheet to need to access external files. For example, you can put commonly used code into Python files in the same directory as your spreadsheet and import them.

You may also want to access access other files; like image files to display in image worksheets, or data to parse and use in your spreadsheet.

Just like normal Python, user code has the __file__ variable available to it. Rather than pointing to a Python file, it contains the full path to the current spreadsheet (the '.rsl' file).

Warning

If the current spreadsheet hasn't yet been saved, __file__ will be set to None.

The following code uses __file__ to read test.txt from the same directory as the current spreadsheet file.

from System.IO import Path
if __file__ is None:
    raise Exception('Save the Spreadsheet!')

spreadsheetDirectory = Path.GetDirectoryName(__file__)
filePath = Path.Combine(spreadsheetDirectory, 'test.txt')

handle = open(filepath)
data = handle.read()
handle.close()

The Python Cookbook has some uesful information on Working with File Paths using .NET classes. Alternatively you can use the os.path module from the Python standard library.


Hosted by Webfaction

Return to Top

Last edited Sat Jun 09 23:52:02 2007.

Copyright ©2007 Michael Foord. All rights reserved. Design by Elemental Works. Logo by FuchsiaShock. Valid XHTML & CSS.

This work is licensed under a Creative Commons License.