我使用pygame和PIL制作了一个图形排序算法,它在Ubuntu(Linux)上工作正常,但每当我在Windows上运行它(我在Windows 7和Windows 10上都运行它)它只能得到它在我收到随机错误之前通过程序的一些方法。关于它的奇怪部分是,它发生在一个循环中,并且循环将始终在错误发生之前运行多次。
保存时发生错误。以下是一些例子:
随机错误#1:
Traceback (most recent call last):
File "C:\Users\dylan\Desktop\sorting (graphical).py", line 13, in <module>
square = Image.open(square_location)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2280, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'square.jpg'
随机错误#2:
Traceback (most recent call last):
File "C:\Users\dylan\Desktop\sorting (graphical).py", line 52, in <module>
square.save(square_location)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 1682, in save
fp = builtins.open(filename, "wb")
OSError: [Errno 22] Invalid argument: 'square.jpg'
随机错误#3:
Traceback (most recent call last):
File "C:\Users\dylan\Desktop\sorting (graphical).py", line 103, in <module>
square.save(square_location)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 1682, in save
fp = builtins.open(filename, "wb")
PermissionError: [Errno 13] Permission denied: 'square.jpg'
有没有人有任何想法可能会发生这种情况?
如果你想知道:
Python 3.4.3
pygame 1.9.2b1
枕头3.3.1
修改:
我已使用以下代码重新创建了正在发生的事情。
import pygame, random
from pygame.locals import *
from pygame.display import flip
from PIL import Image
screen = pygame.display.set_mode((998, 549))
while True:
screen.fill((0, 0, 0))
data = list()
for i in range(83): data.append(random.randint(1, 183))
for value in range(83):
square = Image.open('square.jpg')
square = square.resize((10, 3 * data[value]))
square.save('square.jpg')
square = pygame.image.load('square.jpg')
screen.blit(square, (12 * value + 2, 549 - 3 * data[value]))
flip()
现在,如果您运行此代码以自行查看,请运行几次以便您看到错误都是随机的。此外,可能需要几秒钟才能显示错误。