python无法打开_Python 2.6:“无法打开图像”错误

1586010002-jmsa.png

I'm trying to make the base for a map that I'm going to be using for a game, but I can't load the image onto the screen. I've tried sending in an absolute filepath to the image with likesame letter case, I've tried changing the name of the image, I've tried loading different images that worked in other programs I made, and I've tried putting the image in the same directory as the script itself. Nothing has worked yet. I looked at a few threads of people who were having the same problem, such as Why are my pygame images not loading? and I can't find the answer to my problem. The image will not load. Here's the code:

import sys, pygame

from pygame.locals import *

pygame.init()

size = (600, 400)

screen = pygame.display.set_mode(size)

pygame.display.set_caption("Practice Map")

walls = pygame.image.load("C:\Users\dylan\Desktop\Practice Game\brick.jpg")

x = 0

y = 0

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

if event.type == KEYDOWN and event.key == K_ESCAPE:

sys.exit()

if event.type == KEYDOWN and event.key == K_UP:

y -= 20

if event.type == KEYDOWN and event.key == K_DOWN:

y += 20

if event.type == KEYDOWN and event.key == K_LEFT:

x -= 20

if event.type == KEYDOWN and event.key == K_RIGHT:

x += 20

screen.fill((0,0,0))

screen.blit(walls,(x, 330))

# more bricks to go here later

pygame.display.flip()

#end

and the error:

Traceback (most recent call last):

File "C:\Users\dylan\Desktop\Practice Game\move.py", line 8, in

walls = pygame.image.load("C:\Users\dylan\Desktop\Practice Game\brick.jpg")

error: Couldn't open C:\Users\dylan\Desktop\Practice Gamerick.jpg

I'm using Python 2.6 with PyGame 1.9 for Python version 2.6 with IDLE as my editor.

解决方案

The problem here is that you're using \ as a pathname separator, but \ is also used as an escape character in Python strings. In particular, \b means "backspace" (or '\x08'). You get away with the other backslashes because of not-quite-documented-but-reliable behavior that unknown escape sequences like \X are treated as a backslash followed by an X.

There are three solutions:

Use a raw string, which means Python string escapes are ignored: r"C:\Users\dylan\Desktop\Practice Game\brick.jpg".

Escape your backslashes: "C:\\Users\\dylan\\Desktop\\Practice Game\\brick.jpg".

Use forward slashes instead: "C:/Users/dylan/Desktop/Practice Game/brick.jpg".

If you've memorized the list of Python escape sequences, and are willing to rely on functionality that may change but probably won't, you can get away with only escaping the \b here, but it should be clear why the other three are better ideas in the long run.

While Windows pathnames do natively use backslash separators, all built-in and standard-library Python functions, and most functions in third-party libraries, are perfectly happy to let you use forward slashes instead. (This works because Windows doesn't allow forward slashes in paths at all.)

To understand how and why this works, you might want to try printing out the strings:

>>> print "C:\Users\dylan\Desktop\Practice Game\brick.jpg"

C:\Users\dylan\Desktop\Practice Gamrick.jpg

>>> print r"C:\Users\dylan\Desktop\Practice Game\brick.jpg"

C:\Users\dylan\Desktop\Practice Game\brick.jpg

>>> print "C:\\Users\\dylan\\Desktop\\Practice Game\\brick.jpg"

C:\Users\dylan\Desktop\Practice Game\brick.jpg

>>> print "C:/Users/dylan/Desktop/Practice Game/brick.jpg"

C:/Users/dylan/Desktop/Practice Game/brick.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值