[英]Get filepath from shell working directory in Python?
I'd like to accept a single argument in my script, much like "mkdir". 我想在脚本中接受一个参数,就像“ mkdir”一样。 If the argument is just a name, ie
helloworld
, it would use [pwd]/helloworld
. 如果参数只是一个名称,即
helloworld
,它将使用[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. 如果它包含可以用作文件路径的内容,即
../helloworld
/home/x/helloworld
, ~/helloworld
等,则它将使用这些内容来解析最终路径。 Is there a library like this that exists? 是否存在这样的图书馆? Is Python even capable of getting the working directory of the shell that created it?
Python甚至能够获取创建它的shell的工作目录吗?
EDIT: Never mind the foolish bounty, not sure what caused the problem before, but it's working fine now. 编辑:别介意愚蠢的赏金,不知道是什么原因导致了问题,但是现在工作正常。
I think this is what you're looking for: 我认为这是您要寻找的:
import os
os.path.realpath(__file__)
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. 默认情况下,realpath()不处理tilda,因此您需要expanduser()来完成肮脏的工作。
os.path.expanduser(path)
will expand ~ and ~user to the home directory of user ( http://docs.python.org/2/library/os.path.html ). os.path.expanduser(path)
将〜和os.path.expanduser(path)
扩展到用户的主目录( http://docs.python.org/2/library/os.path.html )。
os.getcwd()
will get you the current (present) working directory. os.getcwd()
将为您提供当前(当前)的工作目录。
os.path.realpath(__file__)
will return the directory where the Python script is. os.path.realpath(__file__)
将返回Python脚本所在的目录。
I think You are interested in os.getcwd()
function. 我认为您对
os.getcwd()
函数感兴趣。 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. 或者可以使用
os.getcwdu()
获得unicode结果。 It returns a unicode string representing the current working directory. 它返回一个代表当前工作目录的unicode字符串。
>>> import os
>>> os.getcwdu()
u'/home/user/work'
You might want to consider using Unipath. 您可能要考虑使用Unipath。 Has many helper functions for path calculations as well as string-path calculations (subclasses str or unicode)
具有许多用于路径计算以及字符串路径计算的帮助器功能(子类str或unicode)
This is the original one for Unix: https://github.com/mikeorr/Unipath 这是Unix的原始版本: https : //github.com/mikeorr/Unipath
This one works on windows too (I made some fixes to it): https://github.com/sesas/Unipath 这个也可以在Windows上运行(我对此做了一些修复): 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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.