PDA

View Full Version : Python Data Libary



Mr.Macdonald
June 2nd, 2008, 04:21 AM
If i were to make a python file (test.py) that were part of a package

i want a statement (function, not and executable script) that, if in test.py, will produce a file that is in the same folder as test.py

to do this i must not put an absolute path, because the package should be movable.

Here is what i want


>>> import testpack
>>> testpack.test.func('hello')
>>> exit()

$ls ./testpack/
test.py test.pyc hello.txt


Hope you understand that

amingv
June 2nd, 2008, 04:29 AM
file_object = open('hello.txt', 'w') #just plain hello.txt, with nothing else
file_object.write('This is written in the same place where the script is')
file_object.close()

Is that about right?

solarwind
June 2nd, 2008, 04:31 AM
Just put a ./ to make the path relative.

Mr.Macdonald
June 3rd, 2008, 02:57 AM
yes all that works when you are using a single file or command line. But not if you want it relative to the script you are importing, "key word". try and get the psuedocode from my last post to work

solarwind
June 3rd, 2008, 03:20 AM
yes all that works when you are using a single file or command line. But not if you want it relative to the script you are importing, "key word". try and get the psuedocode from my last post to work

You didn't explain your question properly. I don't get what you're trying to do...

Mr.Macdonald
June 3rd, 2008, 04:19 AM
Yeah thats what i guessed.

say i had a folder ~/testpack/
and in testpack were a file ~/testpack/test.py
and in testpack their is a function testpack.test.func()

what would i write in that function to make it, when imported (import testpack.test), open a file in "~/testpack/" and write to it without relative pathing (relative to test.py).


so that i could follow these steps and get these results



$cd ~
$python

>>> import testpack.test
>>> testpack.test.func()
>>> exit()

$ls ~/testpack/
test.py __init__.py junk.txt



I really don't know how to make it clearer that this, Sorry

EXCiD3
June 3rd, 2008, 04:20 AM
So you are saying that...for example, say you import os, you would want it to output in the directory the os module is located?

solarwind
June 3rd, 2008, 04:24 AM
Yeah thats what i guessed.

say i had a folder ~/testpack/
and in testpack were a file ~/testpack/test.py
and in testpack their is a function testpack.test.func()

what would i write in that function to make it, when imported (import testpack.test), open a file in "~/testpack/" and write to it without relative pathing (relative to test.py).


so that i could follow these steps and get these results



$cd ~
$python

>>> import testpack.test
>>> testpack.test.func()
>>> exit()

$ls ~/testpack/
test.py __init__.py junk.txt



I really don't know how to make it clearer that this, Sorry

Well its either relative or absolute paths. I don't get what you want. What does the function do?

stroyan
June 3rd, 2008, 05:00 AM
It looks like the inspect module and getabsfile function will give you
what you want.



import inspect

def code_directory():
filename=inspect.getabsfile(code_directory)
dir=filename[0:filename.rfind('/')]
return dir

print code_directory()

Mr.Macdonald
June 3rd, 2008, 03:14 PM
It looks like the inspect module and getabsfile function will give you
what you want.


Code:

import inspect

def code_directory():
filename=inspect.getabsfile(code_directory)
dir=filename[0:filename.rfind('/')]
return dir

print code_directory()



I can't test it yet but this looks right, thanks