在 Python 中向上移动一个目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17885516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Moving up one directory in Python
提问by user2165857
Is there a simple way to move up one directory in python using a single line of code? Something similar to cd ..
in command line
有没有一种简单的方法可以使用一行代码在 python 中向上移动一个目录?类似于cd ..
在命令行中的东西
采纳答案by Ryan G
>>> import os
>>> print os.path.abspath(os.curdir)
C:\Python27
>>> os.chdir("..")
>>> print os.path.abspath(os.curdir)
C:\
回答by Steve Allison
Use the os
module:
使用os
模块:
import os
os.chdir('..')
should work
应该管用
回答by Conan Li
Obviously that os.chdir('..') is the right answer here. But just FYI, if in the future you come across situation when you have to extensively manipulate directories and paths, here is a great package (Unipath) which lets you treat them as Python objects: https://pypi.python.org/pypi/Unipath
显然 os.chdir('..') 是这里的正确答案。但仅供参考,如果将来您遇到必须广泛操作目录和路径的情况,这里有一个很棒的包(Unipath),可让您将它们视为 Python 对象:https: //pypi.python.org/pypi /单路径
so that you could do something like this:
这样你就可以做这样的事情:
>>> from unipath import Path
>>> p = Path("/usr/lib/python2.5/gopherlib.py")
>>> p.parent
Path("/usr/lib/python2.5")
>>> p.name
Path("gopherlib.py")
>>> p.ext
'.py'
回答by aychedee
Well.. I'm not sure how portable os.chdir('..') would actually be. Under Unix those are real filenames. I would prefer the following:
嗯.. 我不确定 os.chdir('..') 的可移植性如何。在 Unix 下,这些是真正的文件名。我更喜欢以下内容:
import os
os.chdir(os.path.dirname(os.getcwd()))
That gets the current working directory, steps up one directory, and then changes to that directory.
获取当前工作目录,上一级目录,然后更改到该目录。
回答by Kim
In Python 3.4 pathlibwas introduced:
在 Python 3.4中引入了pathlib:
>>> from pathlib import Path
>>> p = Path('/etc/usr/lib')
>>> p
PosixPath('/etc/usr/lib')
>>> p.parent
PosixPath('/etc/usr')
It also comes with many other helpful features e.g. for joining paths using slashes or easily walking the directory tree.
它还带有许多其他有用的功能,例如使用斜线连接路径或轻松浏览目录树。
For more information refer to the docsor this blog post, which covers the differences between os.path and pathlib.
回答by Radek D
Although this is not exactly what OP meant as this is not super simple, however, when running scripts from Notepad++ the os.getcwd()
method doesn't work as expected. This is what I would do:
虽然这并不完全是 OP 的意思,因为这不是超级简单,但是,当从 Notepad++ 运行脚本时,该os.getcwd()
方法无法按预期工作。这就是我会做的:
import os
# get real current directory (determined by the file location)
curDir, _ = os.path.split(os.path.abspath(__file__))
print(curDir) # print current directory
Define a function like this:
定义一个这样的函数:
def dir_up(path,n): # here 'path' is your path, 'n' is number of dirs up you want to go
for _ in range(n):
path = dir_up(path.rpartition("\")[0], 0) # second argument equal '0' ensures that
# the function iterates proper number of times
return(path)
The use of this function is fairly simple - all you need is your path and number of directories up.
这个函数的使用相当简单——你所需要的只是你的路径和目录数量。
print(dir_up(curDir,3)) # print 3 directories above the current one
The only minus is that it doesn't stop on drive letter, it just will show you empty string.
唯一的缺点是它不会停在驱动器号上,它只会显示空字符串。