Python 在Ubuntu中访问父目录中的文本文件失败

Python 在Ubuntu中访问父目录中的文本文件失败,python,windows,python-2.7,ubuntu,Python,Windows,Python 2.7,Ubuntu,我在包含python文件的文件夹外放置了一个文本文件,即 a.txt folder myPython.py 下面是我使用的代码 for line in open('../a.txt','rb'): print line 我在windows中工作得很好,但是 IOError: [Errno 2] No such file or directory: '../a.txt' 在Ubuntu12.10中,路径的规范有问题吗 更新 当我使用 fn = os.path.join(os.pa

我在包含python文件的文件夹外放置了一个文本文件,即

a.txt
folder
  myPython.py
下面是我使用的代码

for line in open('../a.txt','rb'):
    print line
我在windows中工作得很好,但是

IOError: [Errno 2] No such file or directory: '../a.txt'
在Ubuntu12.10中,路径的规范有问题吗

更新 当我使用

fn = os.path.join(os.path.dirname(__file__), '../a.txt')
for line in open(fn,'rb'):
        print line

我很好奇为什么这是可能的。

好的,我从这个问题中得出结论:

“”打开是相对于当前目录的。。。当前目录默认为在命令行上启动python时的目录

当我将目录(cd)更改为文件夹级别时,它适用于

for line in open('../a.txt','rb'):
    print line

你确定有文件吗?它在ubuntu上对我有用。它对我也有用。@Chillar和谢谢,我更新了一个工作场景的问题。.我正在使用ubuntu 12.10和Python 2.7,是吗?@badc0re谢谢,我更新了这个问题,我正在使用ubuntu 12.10和Python 2.7,是吗?但是这样,
“../../a.txt”
你将进入两个文件夹,从当前位置?这不是特定于Python的,这是几乎所有程序的工作方式。。。您可以从
sys.argv[0]