如何在AppEngine的python的父目录中打开一个文件? [英] How to open a file in the parent directory in python in AppEngine?

查看:122
本文介绍了如何在AppEngine的python的父目录中打开一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在AppEngine的python的父目录中打开一个文件?



我有一个python文件模块/ mod.py,代码如下:

  f = open('../ data.yml')
z = yaml.load(f)
f.close()

data.yml位于模块的父目录中。我得到的错误是:

  IOError:[Errno 13] file not accessible:'../data.yml'

我正在使用AppEngine SDK 1.3.3。

有没有解决这个问题的方法?

解决方案

open 相对于当前进程的工作目录,而不是它从中调用的模块。如果路径必须是模块相对的,请执行以下操作:

  import os.path 
f = open(os.path .dirname(__ file__)+'/../data.yml')


How to open a file in the parent directory in python in AppEngine?

I have a python file module/mod.py with the following code

f = open('../data.yml')
z = yaml.load(f)
f.close()

data.yml is in the parent dir of module. The error I get is

IOError: [Errno 13] file not accessible: '../data.yml'

I am using AppEngine SDK 1.3.3.

Is there a work around for this?

解决方案

The open function operates relative to the current process working directory, not the module it is called from. If the path must be module-relative, do this:

import os.path
f = open(os.path.dirname(__file__) + '/../data.yml')

这篇关于如何在AppEngine的python的父目录中打开一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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