Is it possible to specify the previous directory python?

前端 未结 2 1048
后悔当初
后悔当初 2021-02-14 20:54

I am attempting to make a Python testing script module self-contained directory-wise for scalability and maintainability purposes.

I have done some research and haven\'t

相关标签:
2条回答
  • 2021-02-14 21:32
    path = os.path.dirname(__file__)
    print(path)
    

    will print the CWD of the file, say C:\Users\Test\Documents\CodeRevamp\Joke

    path2 = os.path.dirname(path)
    print(path2)
    

    will print the Parent directory of of the file: C:\Users\Test\Documents\CodeRevamp

    0 讨论(0)
  • 2021-02-14 21:38

    You're looking to change the working directory? The OS module in python has a lot of functions to help with this.

    import os
    os.chdir( path )
    

    path being ".." to go up one directory. If you need to check where you are before/after a change of directory you can issue the getcwd() command:

    mycwd = os.getcwd()
    os.chdir("..")
    #do stuff in parent directory
    os.chdir(mycwd)     # go back where you came from
    
    0 讨论(0)
提交回复
热议问题