用于获取当前工作目录之外的文件的python代码

时间:2014-04-21 05:15:09

标签: python

使用python代码如何获取当前工作目录之外的文件

正在使用e:\names\test.py directory

dirpath = os.path.dirname(os.path.realpath(__file__))
dirpath prints e:\names
e:\images

中的

如何从文件e:\images\imh.png获取路径test.py 我在test.py中对上述路径进行了硬编码,如何在test.py文件中设置相对路径

2 个答案:

答案 0 :(得分:1)

您可以使用:

os.path.split(os.getcwd())[0]获取父目录。

然后,您可以使用:

a=os.path.split(os.getcwd())[0]
os.listdir(a)

列出父目录的内容

此外,这也适用: -

os.listdir(os.pardir)

答案 1 :(得分:1)

    When on, ":autocmd", shell and write commands are not allowed in
    ".vimrc" and ".exrc" in the current directory and map commands are
    displayed.  Switch it off only if you know that you will not run into
    problems, or when the 'exrc' option is off.  On Unix this option is
    only used if the ".vimrc" or ".exrc" is not owned by you.  This can be
    dangerous if the systems allows users to do a "chown".  You better set
    'secure' at the end of your ~/.vimrc then.
    This option cannot be set from a |modeline| or in the |sandbox|, for
    security reasons.