python相对路径读取文件,在python项目中使用相对路径读取文件

Say I have a python project that is structured as follows:

project

/data

test.csv

/package

__init__.py

module.py

main.py

__init__.py:

from .module import test

module.py:

import csv

with open("..data/test.csv") as f:

test = [line for line in csv.reader(f)]

main.py:

import package

print(package.test)

When I run main.py I get the following error:

C:\Users\Patrick\Desktop\project>python main.py

Traceback (most recent call last):

File "main.py", line 1, in

import package

File "C:\Users\Patrick\Desktop\project\package\__init__.py", line 1, in

from .module import test

File "C:\Users\Patrick\Desktop\project\package\module.py", line 3, in

with open("../data/test.csv") as f:

FileNotFoundError: [Errno 2] No such file or directory: '../data/test.csv'

However, if I run module.py from the package directory I get no errors. So it seems that the relative path used in open(...) is only relative to where the originating file is being run from (i.e __name__ == "__main__")? I don't want to use absolute paths. What are some ways to deal with this?

解决方案

Relative paths are relative to current working directory.

If you do not your want your path to be, it must be absolute.

But there is an often used trick to build an absolute path from current script: use its __file__ special attribute:

import csv

import os.path

my_path = os.path.abspath(os.path.dirname(__file__))

path = os.path.join(my_path, "../data/test.csv")

with open(path) as f:

test = list(csv.reader(f))

Note, from python 3.4, __file__ is always absolute for imported modules and you can drop the os.path.abspath part in this example. Not that it is strictly necessary, but it avoids surprises if you change the current working directory at some point and your module was imported using a relative path.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值