0

I would like to use the filepath of where I called Python from, but I have not found the solution for this yet (perhaps I'm bad at searching).

Example:

Contents of foo.py:

import sys
print(sys.path)
$ pwd
/Here
$ python3 folder1/foo.py
'/Here/folder1'

This is the result I currently get, but I would like to have access to '/Here'.

2

2 Answers 2

1

Save it to a variable

import os

pypath = os.getcwd()

In general the os module will be useful for these types of things and if you want to look at all the files in the current directory(path) then its os.listdir("."), note the "." will equal os.listdir()

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

Comments

0

You need to use os.getcwd() from os module. sys.path contains list of paths in which python will search for the modules that you are importing

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.