python路径读不到子文件夹中的文件
map_path = "data/test.png(需要替换的部分)"
if not os.path.exists(map_path):
print("png does not exist in ", map_path)
exit(0)
map = cv2.imread(map_path)
报错后修改路径:
绝对路径、相对路径可以通过pycharm中右键读取
容易报错自查的点:
1.路径中有中文,不会报错找不到文件,但是读出来的值是None
2.路径直接粘贴会识别为转义符,路径前面加个r.
map_path = r"data/test.png(需要替换的部分)"
3.当前路径不对,先查看当前工作目录,如果不对的话
1.右键大文件夹设置为源目录;
2.运行-编辑设置-工作目录
import os
# 打印当前工作目录
print("Current working directory:", os.getcwd())
# 打印脚本所在目录
print("Directory of the script:", os.path.dirname(__file__))
# 检查并打印预期的父目录内容
parent_dir = os.path.dirname(os.path.abspath(__file__))
print("Expected parent directory contents:", os.listdir(parent_dir))
# 如果需要,打印当前工作目录的文件列表
print("Current working directory contents:", os.listdir(os.getcwd()))