从Python中的相对路径中找到绝对路径 [英] finding absolute path from a relative path in python

查看:218
本文介绍了从Python中的相对路径中找到绝对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题几乎就是标题所暗示的.我的研究促使我尝试了以下方法:

my question is pretty much what the title suggests. my research has led me to try something like this:

import os
pathname = os.path.abspath("some/relative/directory")
print pathname

这个问题是,无论何时我做这样的事情,它都会简单地返回我给它的任何相对目录,并在其前面加上我的python目录.例如:

this problem is that whenever i do something like this, it simply returns whatever relative directory i gave it preceded by my python directory. for example:

C:\ Python27 \ some \ relative \ directory

C:\Python27\some\relative\directory

它甚至不是我计算机上的现有目录.我知道python解释器默认情况下会搜索工作目录,但是我想做的是让它在我的整个计算机中搜索包含我指定的部分目录的绝对路径.

which is not even an existing directory on my computer. i understand that the python interpreter searches the working directory by default, but what i'd like to do is have it search my entire computer for an absolute path that contains the partial directory i specify.

我的目的是创建一个exe(使用py2exe),该exe可以在任何计算机上搜索任意目录.在标准库中或通过某些可用模块有没有执行此操作的方法-还是我必须自己实现算法?

the purpose of this is for me to create an exe (using py2exe) that can search for an arbitrary directory on any computer. is there a method for doing this in the standard library or through some available module - or would i have to implement the algorithm myself?

推荐答案

getcwd .最有可能的是,您当前的工作目录根本不是您所期望的.

abspath is based on getcwd. Most likely, your current working directory simply isn't what you expect.

您可以更改启动脚本的代码,手动更改目录,或仅使用 chdir (在Python中).

You can change the code that launches your script, change directories manually, or just use chdir in Python.

这篇关于从Python中的相对路径中找到绝对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆