没有此类文件或目录的相对路径 [英] No such file or directory for relative path
问题描述
我在一个python文件-backend.py中有一个Bottle应用程序。该文件包含以下定义:
I have a bottle application in one python file - backend.py. The file contains these definitions:
variable = {
'field': [f for f in csv.DictReader(open('../data/fields.csv', 'rb'), delimiter=';')]
}
def run_fcgi():
from bottle import FlupFCGIServer
run(port=8080, server=FlupFCGIServer)
if __name__ == "__main__":
run(host='0.0.0.0', port=8087, server='waitress')
当我运行此应用程序时,例如:
When I run this app like:
python backend.py
应用程序是成功启动。
当我由主管作为fcgi应用程序(fcgi.py)运行此应用程序时:
When I run this application as fcgi application (fcgi.py) by supervisor:
#!my_path_to_python
if __name__ == '__main__':
import backend
backend.run_fcgi()
我有一个错误:
Traceback (most recent call last):
File "path_to_my_project/fcgi.py", line 9, in <module>
import backend
File "path_to_my_project/backend.py", line 49, in <module>
'msk': [i for i in csv.DictReader(open('../data/fields.csv', 'rb'), delimiter=';')],
IOError: [Errno 2] No such file or directory: '../data/fields.csv'
任何想法?
推荐答案
我认为最好不要依赖工作目录。并使用相对于使用此路径的文件的路径。我的意思是您可以随时计算路径:
I think It's better not to rely on working directory. and use path relative to file that is uses this path. I mean you can calculate path on the fly:
import os
csv_path = '../data/fields.csv'
csv_path = os.path.join(os.path.dirname(__file__), csv_path)
在这种情况下,您可以在不同的环境上运行脚本。完整路径将用于确保您不依赖于工作目录。
In this case one be able to run your script on different environment. an full path will be used to be sure that You don't depends on working directory.
这篇关于没有此类文件或目录的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!