1

I have written a simple image viewer. It can view the images just fine, but when it tries to shutil.move a .gif file played with QMovie it crashes. Problem is a still open filehandle. How do I close the filehandle while still playing the gif?

This is the display code.

# -*- coding: utf-8 -*-

import shutil
import sys

from PyQt5 import QtCore, QtGui, QtWidgets

currentFile = "B:\\A.gif"  # <=== GIF
newFile = "B:\\B.gif"


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1600, 900)
        MainWindow.setMinimumSize(QtCore.QSize(1600, 900))
        MainWindow.setMaximumSize(QtCore.QSize(1600, 900))
        MainWindow.setWindowTitle("Demo")
        MainWindow.setWindowOpacity(1.0)
        MainWindow.setAutoFillBackground(False)
        MainWindow.setStyleSheet("background: rgb(125, 125, 125);\n"
                                 "font: 10pt \"Verdana\";\n"
                                 "color: rgb(223, 223, 223)")

        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.moveButton = QtWidgets.QPushButton(self.centralwidget)
        self.moveButton.setGeometry(QtCore.QRect(810, 820, 110, 30))
        self.moveButton.setObjectName("moveButton")
        self.moveButton.clicked.connect(self.moveFile)
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(110, 0, 1380, 820))
        self.label.setStyleSheet("background-color: rgb(85, 85, 85);")
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        self.loadImage()

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        self.moveButton.setText(_translate("MainWindow", "Move File"))

    def loadImage(self):
        print(currentFile)
        image_reader = QtGui.QImageReader()
        image_reader.setDecideFormatFromContent(True)
        image_reader.setFileName(currentFile)
        if image_reader.format() == 'gif':
            movie = QtGui.QMovie(currentFile)
            movie.setCacheMode(QtGui.QMovie.CacheAll)
            movie.setScaledSize(self.label.size())
            self.label.setMovie(movie)
            movie.start()
        else:
            image = image_reader.read()
            myPixmap = QtGui.QPixmap.fromImage(image)
            myScaledPixmap = myPixmap.scaled(self.label.size(), QtCore.Qt.KeepAspectRatio)
            self.label.setPixmap(myScaledPixmap)

    def moveFile(self):
        shutil.move(currentFile, newFile)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Complete error message:

B:\demo\A.gif
Traceback (most recent call last):
  File "C:\Users\Zottelchen\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 557, in move
    os.rename(src, real_dst)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'B:\\demo\\A.gif' -> 'B:\\demo\\B.gif'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "mini.py", line 63, in moveFile
    shutil.move(currentFile, newFile)
  File "C:\Users\Zottelchen\AppData\Local\Programs\Python\Python37\lib\shutil.py", line 572, in move
    os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'B:\\demo\\A.gif'

When trying to shutil.move a .gif it just crashes without an error message.

  • I have tested your code and I do not see the problem you are pointing out, you could provide a [mcve] – eyllanesc May 17 '19 at 21:44
  • @eyllanesc I am not sure how I would do that with Qt to be considered minimal. The full code (277 lines) is on my github: https://github.com/Zottelchen/Hot-or-Not/blob/master/hotornot.py – StackHogger May 17 '19 at 22:00
  • You must invest a little time to provide an MCVE, currently the code you provide is not. If you do not provide it, it's hard to help – eyllanesc May 17 '19 at 22:02
  • @eyllanesc Okay, this is the smallest I could do, while still providing a simple gui: https://gist.github.com/Zottelchen/7d347557da27fe93b567cec9d8962ab2 What happens is similiar to my top example that A.gif is copied to B.gif but A.gif can't be deleted. – StackHogger May 17 '19 at 22:17
  • I see that it works correctly, how many times do you press the button? Remember that when you press the button you are moving the gif from A.gif to B.git, if you press for a second time there will no longer be A.gif, so you will have an exception that probably closes your program – eyllanesc May 17 '19 at 22:43
  • I only press the button once and both gifs exists and the program crashes. – StackHogger May 17 '19 at 23:12
  • I find it strange, on the other hand I recommend you execute the code from the CMD, the IDEs often do not handle the exceptions correctly. – eyllanesc May 17 '19 at 23:14
  • If I try this in CMD it says that the file is in use. But it is just in use by python itself, I've checked that with Process Explorer (and also tried different files). – StackHogger May 17 '19 at 23:24
  • I do not think it only says that, in the CMD should give more detail, so I ask him to show the complete error message – eyllanesc May 17 '19 at 23:25
  • This is the full error: https://hastebin.com/raw/kijeyekedu It really just is a permission error, because the file is in use. – StackHogger May 17 '19 at 23:29
  • I recommend you translate the error message – eyllanesc May 17 '19 at 23:35
  • "The process cannot access the file because it is being used by another process: 'B:\\demo\\A.gif' -> 'B:\\demo\\B.gif'" But this little python script is the only thing using that file. – StackHogger May 17 '19 at 23:37
  • Edit the message to add to your post – eyllanesc May 17 '19 at 23:39
  • I've added the translated error the main post. – StackHogger May 18 '19 at 00:14

0 Answers0