0

I'd like to accept a single argument in my script, much like "mkdir". If the argument is just a name, ie helloworld, it would use [pwd]/helloworld. If it contains something that could be taken as a filepath, ie ../helloworld, /home/x/helloworld, ~/helloworld, etc, then it would use those to resolve the final path. Is there a library like this that exists? Is Python even capable of getting the working directory of the shell that created it?

EDIT: Never mind the foolish bounty, not sure what caused the problem before, but it's working fine now.

5 Answers 5

4

I think this is what you're looking for:

import os
os.path.realpath(__file__)
Sign up to request clarification or add additional context in comments.

Comments

2
+50

The way to do it is the following:

os.path.realpath(os.path.expanduser(__file__))

By default, realpath() doesn't handle tildas, so you need the expanduser() to do the dirty work.

Comments

1

os.path.expanduser(path) will expand ~ and ~user to the home directory of user (http://docs.python.org/2/library/os.path.html).

os.getcwd() will get you the current (present) working directory.

os.path.realpath(__file__) will return the directory where the Python script is.

Comments

0

I think You are interested in os.getcwd() function. It returns a string representing the current working directory

>>> import os
>>> os.getcwd()
'/home/user/work'

Or One can use os.getcwdu() for getting unicode result. It returns a unicode string representing the current working directory.

>>> import os
>>> os.getcwdu()
u'/home/user/work'

1 Comment

But that's a very small part of the problem. Turns out I want os.path.realpath, except that it doesn't resolve ~/
0

You might want to consider using Unipath. Has many helper functions for path calculations as well as string-path calculations (subclasses str or unicode)

This is the original one for Unix: https://github.com/mikeorr/Unipath

This one works on windows too (I made some fixes to it): https://github.com/sesas/Unipath

>>> import unipath
>>> p = unipath.Path()
>>> p
Path(u'.')
>>> p.absolute()
Path(u'C:\\Python27\\Lib\\idlelib')
>>> p.child('hello_world')
Path(u'.\\hello_world')
>>> p = unipath.Path(__file__)  # cannot actually do this in IDLE 
>>> dir(p)
['__add__', '__class__', '__contains__', '__delattr__', '__dict__', '__doc__',
 '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', 
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', 
'__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', 
'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 
'__weakref__', '_formatter_field_name_split', '_formatter_parser', '_new_helper', '_walk', 
'absolute', 'ancestor', 'atime', 'auto_norm', 'capitalize', 'center', 'chdir', 'child', 
'chmod', 'components', 'copy', 'copy_stat', 'copy_tree', 'count', 'ctime', 'cwd', 'decode', 
'encode', 'endswith', 'exists', 'expand', 'expand_user', 'expand_vars', 'expandtabs', 
'ext', 'find', 'format', 'index', 'isabsolute', 'isalnum', 'isalpha', 'isdecimal', 
'isdigit', 'isdir', 'isfile', 'islink', 'islower', 'ismount', 'isnumeric', 'isspace', 
'istitle', 'isupper', 'join', 'lexists', 'listdir', 'ljust', 'lower', 'lstat', 'lstrip', 
'mkdir', 'move', 'mtime', 'name', 'needs_update', 'norm', 'norm_case', 'parent', 
'partition', 'pathlib', 'read_file', 'rel_path_to', 'relative', 'remove', 'rename', 
'replace', 'resolve', 'rfind', 'rindex', 'rjust', 'rmdir', 'rmtree', 'rpartition', 
'rsplit', 'rstrip', 'set_times', 'size', 'split', 'split_root', 'splitlines', 'startswith', 
'stat', 'stem', 'strip', 'swapcase', 'title', 'translate', 'upper', 'walk', 'write_file', 
'zfill']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.