FileNotFoundError: [Errno 2] No such file or directory: ‘data.json‘
A.py:def calculate_categories():categories = {}with open('data.json','r',encoding='utf-8') as fp:r = fp.read()r = json.loads(r)['data']['realtime']#50条左右calculate_categories可以正常执行。B.py:categories = de
·
A.py:
def calculate_categories():
categories = {}
with open('data.json','r',encoding='utf-8') as fp:
r = fp.read()
r = json.loads(r)['data']['realtime']#50条左右
calculate_categories
可以正常执行。
B.py:
注:`A.py` 与`B.py`不同目录下
categories = A.calculate_categories()
间接调用A的calculate_categories
函数报错。
比对发现,两次执行都是使用相对路径,使用os.getcwd()
发现,
B中执行路径:D:\Temp\hot_search_ranking\views
A中执行路径:D:\Temp\hot_search_ranking\utils
data.json
位置:D:\Temp\hot_search_ranking\utils\data.json
总结:
这下可以确定,找不到 data.json
是间接调用使用相对路径错误导致的。
更多推荐
所有评论(0)