Created
October 28, 2011 00:18
-
-
Save wrunk/1321279 to your computer and use it in GitHub Desktop.
Get current python file path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Stupid gist by warren runk | |
import os | |
if __name__ == "__main__": | |
full_file_path = os.path.realpath(__file__) | |
print 'Full path to this current file is', full_file_path | |
# A VERY "cowboy" method of dumping file name. | |
# This will leave you with a path withOUT a trailing slash | |
file_base_dir = '/'.join(full_file_path.split('/')[:-1]) | |
print 'File\'s base directory is', file_base_dir | |
# A more sane way of doing the above would be: | |
# Split up the file path from its slashes | |
path_list = full_file_path.split('/') | |
# Remove the final file name | |
path_list.pop() | |
# Recombobulate back into a path withOUT a trailing slash | |
file_base_dir = '/'.join(path_list) | |
print 'File\'s base directory is', file_base_dir | |
# Super cowboy edition: | |
print 'File\'s base directory is', \ | |
'/'.join(os.path.realpath(__file__).split('/')[:-1]) | |
# Of course you could cheat and do things the correct way :) | |
print 'File\'s base directory is', \ | |
os.path.dirname(os.path.realpath(__file__)) | |
# Note since we are using __file__ as the base element here, we can | |
# skip using os.path.normpath | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment