python os.path.realpath无法正常工作 [英] python os.path.realpath not working properly

查看:144
本文介绍了python os.path.realpath无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

os.chdir(os.path.dirname(os.path.realpath(__file__)) + "/../test")
path.append(os.getcwd())
os.chdir(os.path.dirname(os.path.realpath(__file__)))

应该将/../test添加到python路径中,并且这样做,然后在使用PyDev的eclipse上,它们都可以顺利运行.

但是当我从第二个控制台os.chdir午餐相同的应用程序做错了什么时,实际上在os.path.realpath(__file__)中出现了错误的事情,它返回了../test/myFile.py而不是../originalFolder/myFile.py.当然,我可以通过使用固定的os.chdir("../originalFolder")来解决此问题,但是这对我来说似乎有点不对劲,但这在eclipse和console上都适用.

P.S.我之所以使用os.getcwd(),实际上是因为我想确保没有添加该文件夹,否则我根本就不需要切换目录

那么我的方法有什么问题还是我搞砸了?要不然是啥? :)

提前谢谢! :)

解决方案

看看__file__的值是什么.它不包含脚本的绝对路径,它是命令行中的值,因此可能类似于"./myFile.py"或"myFile.py".另外,realpath()不会使路径绝对,因此在不同目录中调用的realpath("myFile.py")仍将返回"myFile.py".

我认为您应该这样做:

import os.path

script_dir = os.path.dirname(os.path.abspath(__file__))
target_dir = os.path.join(script_dir, '..', 'test')
print(os.getcwd())
os.chdir(target_dir)
print(os.getcwd())
os.chdir(script_dir)
print(os.getcwd())

在我的计算机(Windows)上,结果如下:

e:\parser>c:\Python27\python.exe .\rp.py
e:\parser
e:\test
e:\parser

e:\parser>c:\Python27\python.exe ..\parser\rp.py
e:\parser
e:\test
e:\parser

注意:如果您出于兼容性考虑(不喜欢奇怪的路径错误),则每当合并路径时都应使用 os.path.join(). /p>

注意:我知道我的解决方案很简单(记住绝对路径),但是有时候最简单的解决方案是最好的.

I have following code:

os.chdir(os.path.dirname(os.path.realpath(__file__)) + "/../test")
path.append(os.getcwd())
os.chdir(os.path.dirname(os.path.realpath(__file__)))

Which should add /../test to python path, and it does so, and it all runs smoothly afterwards on eclipse using PyDev.

But when I lunch same app from console second os.chdir does something wrong, actually the wrong thing is in os.path.realpath(__file__) cus it returns ../test/myFile.py in stead of ../originalFolder/myFile.py. Of course I can fix this by using fixed os.chdir("../originalFolder") but that seems a bit wrong to me, but this works on both, eclipse and console.

P.S. I'm using os.getcwd() actually because I want to make sure there isn't such folder already added, otherwise I wouldn't have to switch dir's at all

So is there anything wrong with my approach or I have messed something up? or what? :)

Thanks in advance! :)

解决方案

Take a look what is value of __file__. It doesn't contain absolute path to your script, it's a value from command line, so it may be something like "./myFile.py" or "myFile.py". Also, realpath() doesn't make path absolute, so realpath("myFile.py") called in different directory will still return "myFile.py".

I think you should do ssomething like this:

import os.path

script_dir = os.path.dirname(os.path.abspath(__file__))
target_dir = os.path.join(script_dir, '..', 'test')
print(os.getcwd())
os.chdir(target_dir)
print(os.getcwd())
os.chdir(script_dir)
print(os.getcwd())

On my computer (Windows) I have result like that:

e:\parser>c:\Python27\python.exe .\rp.py
e:\parser
e:\test
e:\parser

e:\parser>c:\Python27\python.exe ..\parser\rp.py
e:\parser
e:\test
e:\parser

Note: If you care for compatibility (you don't like strange path errors) you should use os.path.join() whenever you combine paths.

Note: I know my solution is dead simple (remember absolute path), but sometimes simplest solutions are best.

这篇关于python os.path.realpath无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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