[英]os.chdir() to relative home directory (/home/usr/)
Is there a way to use os.chdir() to go to relative user folder? 有没有办法使用os.chdir()去相关用户文件夹?
I'm making a bash and the only issue I found is the cd ~
, arg[0]
is undefined since I'm using this cd functions: 我正在做一个bash,我发现的唯一问题是
cd ~
, arg[0]
是未定义的,因为我正在使用这个cd函数:
def cd(args):
os.chdir(args[0])
return current_status
Which I want to change to 我想改变的
def cd(args):
if args[0] == '~':
os.chdir('/home/')
# Here I left it to /home/ since I don't know how
# to get the user's folder name
else:
os.chdir(args[0])
return current_status
No, os.chdir
won't do that, since it is just a thin wrapper around a system call. 不,
os.chdir
不会这样做,因为它只是系统调用的一个薄包装器。 Consider that ~
is actually a legal name for a directory. 考虑到
~
实际上是目录的合法名称。
You can, however, use os.expanduser
to expand ~
in a path. 但是,您可以使用
os.expanduser
在路径中展开~
。
def cd(path):
os.chdir(os.path.expanduser(path))
Note that this will also expand ~user
to the home directory for user
. 请注意,这也将扩大
~user
的主目录user
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.