python - Pull files into script from two directories back -
I have a script that pulls files from two directories, so the script remains on:
< P> / folder2 / folder1 / folder0 / script.py
and the files that will be processed will be in folder 2.
I can get a level back "..//" (I'm creating an executable Windows with cx_free), but I'm thinking this is not the best way to do this.
I have an input directory and an output directory. I want to keep the paths relative to the location of the script so that the "Folder 2" script can be moved without screwing the functionality of the script or without the force of writing it again.
Thank you
First you get the directory where your script is located, such as :
current_dir = os Path.dirname (os.path.realpath (__ file__))
And then, if you know that you always above Use the given two levels of directory, then use it:
target_dir = os.path.join (current_dir, '..', '..')
< / Pre>From there you can manipulate files from target_dir as you please
Edit
Rather than joining the two ".." paths together, you can define target_dir instead:
Target_dir = os.path.sep.join (current_dir.split (os.path.sep) [: - 2])
Which only last in your way Instead of ending in some notorious ".." S instead of the two directories, the first method looks like this:
/>
/ path / to / folder2 / folder1 / directory The second implementation will be:
/ path / to / folder2 /
Comments
Post a Comment