relative directory python

Hi: I want to read files from the directory beneath that where the python program resides.
This is in windows XP
The code reads for the current directory:
files = [f for f in os.listdir('.') if os.path.isfile(f)]
but I want to read the directory 'import' off the current.
I have tried /import; /import/;./import; and ./import/!
I either get a warning that directory does not exist or the program executes with nothing returned. It does work when I copy the files into the current directory so the program is OK!
The directories and files are stored on a server, the program run from a local PC
Does anybody know how to set a different directory when reading in python?
Richard TeasdaleFinancial ControllerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gelonidaCommented:
There are two different directories.
1) The directory in which your python script resides in
2) the current working directory

these directories are not necessarily the same though the are in many cases

For 1)
import os
mydir = os.dirname(__file__)
import_dir = os.path.join(mydir, 'import')
# next line only required of your script changes the working directory
import_dir = os.path.realpath(import_dir)

Open in new window


for 2)
import_dir = 'import'

Open in new window


and then
files = [f for f in os.listdir(import_dir) 
    if os.path.isfile(os.path.join(import_dir, f))]

Open in new window


as you have to check the files presence in the directory you're interested in and not in the current dir.


Depending on your exact needs you might also consider looking at
os.walk(), which directly separates files and directories, though it descends recursively into any other subdirectories
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard TeasdaleFinancial ControllerAuthor Commented:
Thank you very much!
0
peprCommented:
... should be os.path.dirname(__file__)
0
gelonidaCommented:
thanks pepr. You always get me if I post unverified code ;-)
0
peprCommented:
Oh, no! I was just surprised the function exits (and searched in the doc). It looked a bit strange to me ;)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.