作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我通过 Visual Studio Code 加载文件时,VScode 找不到目录。
我可以在终端上毫无问题地运行代码,结果是:
young@young-desktop:/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials/10_RNN/ChatBot$ python text_load_text.py
['fij\n', 'feijfaef\n', 'ef\n', 'awef\n', 'awe\n', 'g\n', 'aweg\n', 'ae\n', 'wg\n', 'awe\n', 'h\n', 'aw\n', 'h\n', 'aw\n', 'ef\n', 'aweg\n', 'wea\n', 'gaw\n', 'eg\n', '\n']
young@young-desktop:/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials$ cd /media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials ; env "PYTHONIOENCODING=UTF-8" "PYTHONUNBUFFERED=1" /home/young/anaconda3/bin/python /home/young/.vscode/extensions/ms-python.python-2018.6.0/pythonFiles/PythonTools/visualstudio_py_launcher.py /media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials 39707 34806ad9-833a-4524-8cd6-18ca4aa74f14 RedirectOutput,RedirectOutput /media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials/10_RNN/ChatBot/text_load_text.py
Traceback (most recent call last):
File "/media/young/5e7be152-8ed5-483d-a8e8-b3fecfa221dc/text/mycodehtml/pracdl/golbin/TensorFlow-Tutorials/10_RNN/ChatBot/text_load_text.py", line 8, in <module>
with open('test.txt', 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
with open('./data/test.txt', 'r') as f:
with open('test.txt', 'r') as f:
with open('./test.txt', 'r') as f:
最佳答案
首先通过os.path.dirname(__file__)
获取当前运行的模块的目录.
然后,您可以将您的愿望文件的相对路径加入此目录路径。
from os.path import dirname, join
current_dir = dirname(__file__)
file_path = join(current_dir, "./test.txt")
with open(file_path, 'r') as f:
关于path - Visual Studio Code : FileNotFoundError: [Errno 2] No such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51006989/
我是一名优秀的程序员,十分优秀!