Python 在windows上使用PIL时出现随机错误

Python 在windows上使用PIL时出现随机错误,python,python-imaging-library,pillow,Python,Python Imaging Library,Pillow,我用pygame和PIL制作了一个图形排序算法,它在Ubuntu(Linux)上运行得很好,但每当我在Windows上运行它(我在Windows 7和Windows 10上都运行过)时,它只能在程序中运行一段时间,然后我就会出现随机错误。奇怪的是,它发生在一个循环中,在错误发生之前,循环总是会运行多次 保存时出错。以下是一些例子: 随机误差#1: 现在,如果您运行这段代码是为了亲自查看,请运行几次,以便看到错误都是随机的。此外,可能需要几秒钟才能显示错误。如果发布代码的相关部分,将更容易提供帮助

我用pygame和PIL制作了一个图形排序算法,它在Ubuntu(Linux)上运行得很好,但每当我在Windows上运行它(我在Windows 7和Windows 10上都运行过)时,它只能在程序中运行一段时间,然后我就会出现随机错误。奇怪的是,它发生在一个循环中,在错误发生之前,循环总是会运行多次

保存时出错。以下是一些例子: 随机误差#1:


现在,如果您运行这段代码是为了亲自查看,请运行几次,以便看到错误都是随机的。此外,可能需要几秒钟才能显示错误。

如果发布代码的相关部分,将更容易提供帮助。您将丢失
python路径定义。工作目录永远不是系统目录。给定完整路径或
path=os.getcwd()+“\\”+文件名
dsgdfg,我尝试了完整目录,结果没有变化。不过,感谢您的想法。如果您发布代码的相关部分,将更容易提供帮助。您将丢失
python路径定义
。工作目录永远不是系统目录。给定完整路径或
path=os.getcwd()+“\\”+文件名
dsgdfg,我尝试了完整目录,结果没有变化。不过谢谢你的主意。
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'
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'
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'
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()