2

I am working on the files with different locations. I want to avoid hard coding at every step. My files Involves, Input files(consisting of inputs), python scripts and output files. Now, I want to interact with each of the mentioned folder without hard-coding my scripts. Does python has any module for trimming down the path to root directory and accessing the subsequent folders.

Currently, I am using os.getcwd() for accessing my script's path. I want to use the output of this code for accessing other files in the different folder

import os
dirpath = os.getcwd()
root_dirpath, py_script = os.path.split(dirpath)

root_dirpath in the code gives me the path for my python script. I want to trim this path to user folder and then access the file from the other folder. Is there any module to do this with out actually hard coding.

6
  • look in to pathlib from the stdlib as well as the __file__ statement Commented Apr 30, 2019 at 16:49
  • getcwd only returns your script's directory if you run your code from that directory. Commented Apr 30, 2019 at 16:53
  • os.listdir(PATH) will list everything in the PATH, so you can loop through the contents of that. How many levels deep do you need to go from the root_dirpath? Commented Apr 30, 2019 at 17:05
  • @ekmcd I want to go 2 levels back. In "'os.listdir(PATH)''' do i need to specify the path! or is there a way to get the path for the root directory. Commented Apr 30, 2019 at 17:35
  • If by root dir/ user folder, you are referring to the home directory of the current user, then os.environ['HOME'] should give you that. Then you can use os.listdir(os.environ['HOME']) to list every file and sub_directory in os.environ['HOME'] Commented Apr 30, 2019 at 17:58

1 Answer 1

2

There is current-working-directory, and then there is the directory your program is in. They are distinct concepts, and might have the same value or different.

This expression is pretty useful:

os.path.dirname(os.path.realpath(__file__))

Or more simply, just take dirname of __file__ if your $PYTHONPATH lacks troublesome entries like ..

Some projects will have a "top" directory that is one or more levels up from the location of your python source. You might find it useful to append '../../..' to __file__, or use os.path.join() to accomplish the same thing.

Here is one possibility for locating your favorite files:

from pathlib import Path

top = Path(__file__ + '../..').resolve()
arrow = top / 'assets/arrow.png'
print('source image:', arrow)
print('destination image:', top / 'output' / os.path.basename(arrow))

Caveat: sometimes library code runs directly from a .zip file or other unusual environment. In that case you might find __file__ is not quite as useful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.