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.
pathlibfrom the stdlib as well as the__file__statementgetcwdonly returns your script's directory if you run your code from that directory.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?os.environ['HOME']should give you that. Then you can useos.listdir(os.environ['HOME'])to list every file and sub_directory inos.environ['HOME']