0

I am trying to write a piece of code that takes the filepath from my jupyter notebook and changes the current directory to its grandparent folder.

I have found multiple solutions that work to address this issue.

1) 
__file__ = os.getcwd()
Main_Folder = path.abspath(path.join(__file__ ,"../.."))
os.chdir(Main_Folder)

the issue with this solution is that you cannot run the code multiple times... because it changes the working directory

2) 
__file__= os.getcwd()
location = input("Enter In the Path",)
offset = len(location)
Main_Folder = __file__[0:__file__.find(location) + offset]

This solution has the same issue as one because its getting the file location from the current directory... :/

3)
Main_Folder = os.path.abspath(os.path.dirname(os.path.realpath('__file__'))+"../../..")

Solution 3 I figured would have been the correct solution, however, for some reason file changes as the directory changes... Im officially fully stumped here and could really use some help from you all!

I am trying to write this code so the line can be run indefinitely, but yield the same result... Which is why I thought referencing file would work.

One side note, I am running this from a jupyter notebook which is why I had to do the funky os.path stuff in step 3 rather than just os.path.abspath(file)

Lou
  • 5
  • 1
  • 4

0 Answers0