python 有关路径实验
下一级目录

这说明 ./utils和.\utils和utils三个写法的作用都是一样的
(pytorch_gpu) PS F:\python\Deep-Learning-Image-Classification-Models-Based-CNN-or-Attention-main> python /utils/train.py
D:\anaconda\envs\pytorch_gpu\python.exe: can't open file 'F:\\utils\\train.py': [Errno 2] No such file or directory
这里说明/在根目录(F盘)下面找utils了
验证:
(pytorch_gpu) PS F:\python\Deep-Learning-Image-Classification-Models-Based-CNN-or-Attention-main\utils> python /utils/test.py
D:\anaconda\envs\pytorch_gpu\python.exe: can't open file 'F:\\utils\\test.py': [Errno 2] No such file or directory
当我再进入下一级目录,然后再使用python /utils/test.py。他找的还是F盘下面的util
上一级目录
(pytorch_gpu) PS F:\python\Deep-Learning-Image-Classification-Models-Based-CNN-or-Attention-main\utils> python ../results/x.py
D:\anaconda\envs\pytorch_gpu\python.exe: can't open file 'F:\\python\\Deep-Learning-Image-Classification-Models-Based-CNN-or-Attention-main\\results\\x.py': [Errno 2] No such file or directory
(pytorch_gpu) PS F:\python\Deep-Learning-Image-Classification-Models-Based-CNN-or-Attention-main\utils> python ..\results/x.py
D:\anaconda\envs\pytorch_gpu\python.exe: can't open file 'F:\\python\\Deep-Learning-Image-Classification-Models-Based-CNN-or-Attention-main\\results\\x.py': [Errno 2] No such file or directory
说明../和..\的作用是一样的
文章通过实验展示了在Python中使用./utils,.\utils和utils引用同一目录的等效性,以及在不同目录层级中使用../和..访问上一级目录的相同效果。然而,当尝试执行文件时,如python/utils/test.py,解释器在文件系统的根目录下查找,导致找不到文件错误。这说明正确引用路径对于程序执行至关重要。

被折叠的 条评论
为什么被折叠?



