selfteaching / selfteaching/selfteaching-python-camp

Day09 运行main.py出现读取不了tang300.json文件错误原因

Open
#855 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

学习心得分享
Dominant language
Python
Stars
151
Forks
875
PR merge metrics
No merged PRs in 30d

Description

因为vs code打开的路径是/User/xxx/19100101/,
当main.py直接读取文件tang300.json的时候,
是默认读取/User/xxx/19100101/tang300.json路径,
而我们的tang300.json实际路径是/User/xxx/19100101/用户名/tang300.json,所以就会报错。
解决方法:vs code ==>文件==>打开(文件)找到/User/xxx/19100101/用户名/,然后打开,再运行main.py就能直接读取tang300.json了。
还有一种教练提供解决方法是:
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tang300.json')) as f :
这样可以读取到文件的绝对路径

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Open main.py and inspect how it opens tang300.json; reproduce the failure when running from /User/xxx/19100101/ instead of the user subdirectory. Done means the script can locate tang300.json using the project path handling described in the issue, and the run succeeds from the stated VS Code location.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.