Having Permission Errors with shutil, pillow and os modules in python

Issue

This Content is from Stack Overflow. Question asked by Hendrik

I’m currently trying to use tensorflow and I have been trying to create a basic python script for images I am going to label. The script i’m trying to create will convert two different file types (.jpg, and .jfif) into .png files by creating new .png files and then take the original files and put them in a different folder. However for some reason no matter what I do or solution I try I keep running into a Permission Error. I am not a python expert so I was hoping someone with a bit more experience could point out my mistake.

I even tried a try except finally block to somehow catch the error and then remove the last file (because for some reason it seemed to copy it to the processed_photos folder, but os.remove() still doesn’t work)

Thanks for your time,

-Hendrik

Here is the error:

PermissionError Traceback (most recent call last)
~AppDataLocalTempipykernel_168722843218146.py in
69 if os.path.exists(source_img_path):
70 print(source_img_path)
—> 71 os.remove(source_img_path)
72
73

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: ‘C:/Users/Hendrik/Desktop/Test/Tensorflow Object Detection/TFODCourse/Tensorflow/workspace/images/collectedimages/raccoon/videothumbnail.1614718479861.jpg’

^ (this ‘videothumbnail.1614718479861.jpg’ is the last .jpg/.jfif image of the directory)

# Import opencv
import cv2 

# Import uuid
import uuid

# Import Operating System
import os

# Import time
import time

# Import Pillow Image Function
from PIL import Image

# Import Shutil

import shutil

labels = ['raccoon']
number_imgs = 24

IMAGES_PATH = os.path.join('Tensorflow', 'workspace', 'images', 'collectedimages')

if not os.path.exists(IMAGES_PATH):
    if os.name == 'posix':
        !mkdir -p {IMAGES_PATH}
    if os.name == 'nt':
         !mkdir {IMAGES_PATH}
for label in labels:
    path = os.path.join(IMAGES_PATH, label)
    if not os.path.exists(path):
        !mkdir {path}

for label in labels:
    i = 0

    for filename in os.listdir(directory): 
        filetypes = ('.jpg', 'jfif')
        if(filename.endswith(filetypes) == True):
            try:
                i = i + 1
                print(i)
                imgname = os.path.join(IMAGES_PATH,label,label+'.'+'{}.png'.format(str(uuid.uuid1())))
                im = Image.open(os.path.join(directory,filename))




                processed_img_dir = 'C:/Users/Hendrik/Desktop/Test/Tensorflow Object Detection/TFODCourse/Tensorflow/workspace/images/processed_photos' 
                processed_img_path = os.path.join(processed_img_dir,name).replace("\","/")

                

                im.save(imgname, quality=100)

                im.close()
                
                if not os.path.exists(processed_img_dir):
                    os.mkdir(processed_img_dir)

                processed_img_path = os.path.join(processed_img_dir,filename).replace("\","/")
                source_img_path =  os.path.join(directory,filename).replace("\","/")

                shutil.move(source_img_path , processed_img_path)
                print(processed_img_path)
                print(source_img_path)
            except PermissionError:
                print('Last File')
                print(source_img_path)
                
                
            finally:
                if os.path.exists(source_img_path):
                    print(source_img_path)
                    os.remove(source_img_path)
                
               
            



Solution

Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?