You can even adopt both things: try finding files in the hardcoded directory first and check the application's current directory if you find nothing.
Yes, but that's the problem: I *want* to look just in the current directory, but the app seems to think the current directory is (on the Mac) inside itself, in the Contents/Resources part of its own bundle.
So is there *any* code that, both on Windows and Mac, in a normal or garden system, will say "look in the dir this app was run from"?
Charles Hartman
Professor of English, Poet in Residence
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com
Charles Hartman <charles.hartman@conncoll.edu> writes:
So is there *any* code that, both on Windows and Mac, in a normal or
garden system, will say "look in the dir this app was run from"?
I don't know one command that does that. I'd try opening in the current
directory (it works on Windows and other OSs but not on Macs) and if the
file isn't there two directories above (it works on Macs).
You can check for the files before opening them with os.stat:
import os
os.stat('test.py')
(33204, 283428L, 771L, 1, 500, 500, 445L, 1100642299, 1100642150, 1100642150)
os.stat('freakazoid')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 2] No such file or directory: 'freakazoid'
So, something like (untested code beyond what is above)
···
======================================================================
import os
try:
if os.stat('datafile'):
datadir = '.'
except OSError:
datadir = '../..'
dataToUse = open(os.sep.join([datadir, 'datafile']))
would open the file no matter what OS you use if it is on the very same
directory of the application itself.
Be seeing you,
--
Godoy. <godoy@ieee.org>
Charles Hartman wrote:
So is there *any* code that, both on Windows and Mac, in a normal or garden system, will say "look in the dir this app was run from"?
No. and I'm no expert, but the reason there isn't is because it's apparently impossible to do reliably on all platforms (search the wx-dev archives for arguments from folks more knowledgeable than me).
That being said, if you know who you are distributing your apps to, you don't need something that's reliable on possible systems. I would NOT use getcwd(), but I might use sys.argv[0]. If your OS-X app is in a bundle, I think you can count on the structure of that bundle, and thus know that the app is three steps up from "Resources".
You'll just need to write a little platform dependent code.
By the way, if you search various archives, there is code that will find the app location most of the time, even on complex *nix systems, though it's still not 100% reliable. I couldn't find it on a quick search, but it's there, I think in wx-dev.
-Chris
···
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@noaa.gov