selfteaching / selfteaching/selfteaching-python-camp
Day09 读取本地文件是否有更好的方式?
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 151
- Forks
- 875
- PR merge metrics
- No merged PRs in 30d
Description
因为同时在两个文件夹写代码,所以工作目录与当前执行的main.py不一定是同一个目录。
有时候,想要打开main.py同目录下的tang300.json文件,写open('tang300.json','r')会报错找不到文件,因为工作目录是另一个文件夹。
想了一个办法如下:
location = sys.argv[0] #获取main.py的路径
location = location.rstrip('main.py') + 'tang300.json' #获取同目录下tang300.json的路径
但是这个方法看起来呆呆的...
想问一下:
除了这样写以外,有什么更简洁的方法打开与main()同目录文件夹下的文件?
搜索引擎的一个方法是修改工作目录,不过好像这也看起来呆呆的...大家都是一个文件夹下的文件,应该有很简单的打开办法吧?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Review the Day09 material and the examples involving main.py and tang300.json. Confirm how the lesson should explain opening a file beside the executing script, and consider the explanation complete when the recommended approach is clearly documented for learners.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100