You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在VS Code中使用Python加载图片时如何指定正确文件路径?

Python Turtle加载GIF图片路径错误问题

问题背景

正在学习Python课程,使用Turtle库开发项目时需要加载一张.gif图片作为背景,代码和讲师视频一致,但运行后报错,推测是路径问题:图片在D盘,但程序提示找不到文件。

我的代码

import turtle 

screen = turtle.Screen() 
screen.title("THE STATES GAME")
# This path copy and pasted using rightclick in VS Explorer
# Started with image = "blank_states_img.gif" 
image = "D:\learnToProgram\Day 25\day25_state_start\blank_states_img.gif"
screen.addshape(image)
turtle.shape(image)

answer = screen.textinput(title="Guess the States", prompt="Give me a state:")

screen.exitonclick()

报错信息

PS D:\learnToProgram\Day 25>  d:; cd 'd:\learnToProgram\Day 25'; & 'C:\Users\lanie\AppData\Local\Programs\Python\Python311\python.exe' 'c:\Users\lanie.vscode\extensions\ms-python.python-2023.18.0\pythonFiles\lib\python\debugpy\adapter/../..\debugpy\launcher' '57939' '--' 'd:\learnToProgram\Day 25\day25_state_start\main.py'
Traceback (most recent call last):
File "d:\learnToProgram\Day 25\day25_state_start\main.py", line 9, in <module>
screen.addshape(image)
File "C:\Users\lanie\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 1134, in register_shape
shape = Shape("image", self._image(name))
^^^^^^^^^^^^^^^^^
File "C:\Users\lanie\AppData\Local\Programs\Python\Python311\Lib\turtle.py", line 479, in image
return TK.PhotoImage(file=filename, master=self.cv)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\lanie\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 4130, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\lanie\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 4075, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "blank_states_img.gif": no such file or directory
PS D:\learnToProgram\Day 25>

需求

  1. 理解文件路径工作原理的核心要点
  2. 在VS Code或Python中无需移动文件的解决办法
  3. 明确该问题的类型

问题归类

这是通用编码中的路径问题,和VS Code无关,本质是Python对Windows路径中反斜杠的转义处理导致路径解析错误。

文件路径核心原理

  • Windows系统用反斜杠\作为路径分隔符,但在Python字符串中,\是转义字符(比如\n代表换行、\t代表制表符),直接写带\的路径会被Python解析成转义序列,导致路径实际内容和预期不符。
  • 绝对路径:从根目录(如D:\)开始的完整路径,能唯一定位文件;相对路径:相对于当前程序运行的工作目录的路径,更灵活。
  • 跨平台路径分隔符:正斜杠/在Windows、Linux、Mac中都能被识别,不会触发转义问题。

无需移动文件的解决办法

1. 使用原始字符串

在路径字符串前加r,让Python忽略所有转义字符,保留路径原样:

image = r"D:\learnToProgram\Day 25\day25_state_start\blank_states_img.gif"

2. 替换反斜杠为正斜杠

直接用/代替\,避免转义问题:

image = "D:/learnToProgram/Day 25/day25_state_start/blank_states_img.gif"

3. 用os模块拼接路径(跨平台推荐)

使用os.path.join()自动适配系统路径分隔符,还能结合脚本自身路径实现相对定位:

import turtle
import os

# 获取当前脚本所在目录
script_dir = os.path.dirname(os.path.abspath(__file__))
# 拼接图片路径(假设图片和脚本在同一目录)
image = os.path.join(script_dir, "blank_states_img.gif")

screen = turtle.Screen() 
screen.title("THE STATES GAME")
screen.addshape(image)
turtle.shape(image)

answer = screen.textinput(title="Guess the States", prompt="Give me a state:")
screen.exitonclick()

这种方法无需写死绝对路径,不管脚本移动到哪里,只要图片和脚本同目录就能正常加载。

内容的提问来源于stack exchange,提问作者Len Kedrow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 01:00:56