tempfile library throwing error when removing temp directory - python

117 Views Asked by At

My code is using a temp folder (TemporaryDirectory from tempfile library) and I am copying files to said temp folder

    with TemporaryDirectory(dir='.', prefix="pyGDriveUpload_temp_") as tempDir:
        tempDir = tempDir + '\\'
        for copyFile in selectedFiles:
            createdTime = os.path.getctime(copyFile)
            fileName = os.path.basename(copyFile)
            shutil.copy2(copyFile, tempDir + (fileName))
            setctime(tempDir + fileName, createdTime)
        uploadList = os.listdir(tempDir)
        # Upload
        uploadCount = len(uploadList)
        progress = 1
        print("\nUploading " + "0" + "/" + str(uploadCount), end='\r')
        for uploadFile in uploadList:
            getTime = datetime.datetime.fromtimestamp(os.path.getctime(tempDir + uploadFile))
            ctime = getTime.isoformat('T') + 'Z'
            if FolderHasBeenProvidedByUser:
                gfile = drive.CreateFile({'parents': [{'id': gFolderID}], 'title': uploadFile, 'createdDate': ctime, 'modifiedDate': ctime})
            elif not FolderHasBeenProvidedByUser:
                gfile = drive.CreateFile({'title': uploadFile, 'createdDate': ctime, 'modifiedDate': ctime})
            # Read file and set it as the content of this instance.
            gfile.SetContentFile(os.path.abspath(tempDir) + '\\' + uploadFile)
            gfile.Upload() # Upload the file.
            print("Uploading " + str(progress) + "/" + str(uploadCount), end='\r')
            progress += 1
        print("Files Successfully Uploaded! Closing...", end='')
        winsound.PlaySound("C:\Windows\Media\Windows Balloon.wav", winsound.SND_ALIAS)
        sleep(1.5)
    quit()

I am using this to upload files to google drive, and after the upload code I have it quit().

The traceback is as follows:

Traceback (most recent call last):
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 617, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '.\\pyGDriveUpload_temp_rhz30vlz\\video.mkv'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\tempfile.py", line 843, in onerror
    _os.unlink(path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '.\\pyGDriveUpload_temp_rhz30vlz\\video.mkv'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\mjc01\Documents\My Better Scripting\programs\final\Google Drive Uploader\pythonGDriveUpload.py", line 72, in <module>
    with TemporaryDirectory(dir='.', prefix="pyGDriveUpload_temp_") as tempDir:
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\tempfile.py", line 869, in __exit__
    self.cleanup()
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\tempfile.py", line 873, in cleanup
    self._rmtree(self.name, ignore_errors=self._ignore_cleanup_errors)
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\tempfile.py", line 855, in _rmtree
    _shutil.rmtree(name, onerror=onerror)
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 749, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 619, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\tempfile.py", line 846, in onerror
    cls._rmtree(path, ignore_errors=ignore_errors)
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\tempfile.py", line 855, in _rmtree
    _shutil.rmtree(name, onerror=onerror)
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 749, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 600, in _rmtree_unsafe
    onerror(os.scandir, path, sys.exc_info())
  File "C:\Users\mjc01\AppData\Local\Programs\Python\Python310\lib\shutil.py", line 597, in _rmtree_unsafe
    with os.scandir(path) as scandir_it:
NotADirectoryError: [WinError 267] The directory name is invalid: '.\\pyGDriveUpload_temp_rhz30vlz\\video.mkv'

Everything works fine except for the temp folder deleting itself (I cant really understand the traceback very well and was not expecting any type of error)

0

There are 0 best solutions below