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'.

jwodder
  • 46,316
  • 9
  • 83
  • 106
Travis
  • 1
  • 6
    Try `os.getcwd()` – jmetz Oct 30 '17 at 21:38
  • Possible duplicate of [How to properly determine current script directory in Python?](https://stackoverflow.com/questions/3718657/how-to-properly-determine-current-script-directory-in-python) – hamidfzm Oct 30 '17 at 21:43

2 Answers2

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()

atomsmasher
  • 623
  • 6
  • 19
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

yash
  • 1,187
  • 1
  • 19
  • 31