1

I'm setting up a python script to organize my Downloads Folder, but every time a file is downloaded, or when I open a folder inside the Downloads folder, i get the Following Errors:

Traceback (most recent call last):
  File "D:\Apps\Anaconda\lib\shutil.py", line 788, in move
    os.rename(src, real_dst)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'D:\\Downloads\\cd4d05fb-407e-4d59-996b-26d86a20a44c.tmp' -> 'D:\\Downloads\\tmp\\cd4d05fb-407e-4d59-996b-26d86a20a44c.tmp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Documents/Shortcuts/Folder Organizer.pyw", line 29, in <module>
    shutil.move("D:\Downloads\{}".format(arquivo), "D:\Downloads\{}".format(ext))
  File "D:\Apps\Anaconda\lib\shutil.py", line 803, in move
    os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'D:\\Downloads\\cd4d05fb-407e-4d59-996b-26d86a20a44c.tmp'

Here is my code atm:

import os
import shutil
from mutagen.easyid3 import EasyID3
while True:
    directory = os.listdir("D:\Downloads")
    for arquivo in directory:
        directory = os.listdir("D:\Downloads")
        filename = arquivo.split(".")
        ext = filename[-1]
        filename = arquivo.split("-")
        if filename[0] == "MEGA":
            year = filename[3].split(".")
            year = year[0]
            shutil.move("D:\Downloads\{}".format(arquivo), "D:\Music\MEGA")
            audiofile = EasyID3("D:\Music\MEGA\{}".format(arquivo))
            audiofile["title"] = u"{}".format(filename[1])
            audiofile["album"] = u"MEGA"
            audiofile["artist"] = u"{}".format(filename[2])
            if filename[-1] != filename[3]:
                audiofile["albumartist"] = u"{}".format(filename[4])
            audiofile.save()
        elif ext == "mp3":
            shutil.move("D:\Downloads\{}".format(arquivo), "D:\Music")
        elif ext == "mp4":
            shutil.move("D:\Downloads\{}".format(arquivo), "D:\Videos")
        else:
            if ext not in directory:
                os.mkdir("D:\Downloads\{}".format(ext))
            shutil.move("D:\Downloads\{}".format(arquivo), "D:\Downloads\{}".format(ext))

The while in the beginning is because I want to make it into a process that runs in Windows background, thought that would be the way to make it run every time a new file is put into the Downloads folder

I am aware that there are programs out there that organize folders like this automatically, but I wanted to set it up in Python as challenge for myself, got kind of stuck in these errors.

EDIT: Changed the code a little to handle exceptions, also added a timer so the script wouldn't just run as many times as it was. Also added a piece of code from the website : http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html under "Poll the directory with os.listdir". It is now running as I intendet it to. Code is as follows:

import os
import shutil
from mutagen.easyid3 import EasyID3
import time

directory = "D:/Downloads"
before = dict ([(f, None) for f in os.listdir (directory)])
while True:
    time.sleep (1)
    after = dict ([(f, None) for f in os.listdir (directory)])
    added = [f for f in after if not f in before]
    before = after
    print("added: {}".format(added))
    try:
        if added:
            filelist = os.listdir("D:/Downloads")
            for item in filelist:
                print("entrou")
                allfiles = os.listdir("D:\Downloads")
                filename = item.split(".")
                ext = filename[-1]
                filename = item.split("-")
                if filename[0] == "MEGA":
                    year = filename[3].split(".")
                    year = year[0]
                    shutil.move("D:\Downloads\{}".format(item), "D:\Music\MEGA")
                    audiofile = EasyID3("D:\Music\MEGA\{}".format(item))
                    audiofile["title"] = u"{}".format(filename[1])
                    audiofile["album"] = u"MEGA"
                    audiofile["artist"] = u"{}".format(filename[2])
                    if filename[-1] != filename[3]:
                        audiofile["albumartist"] = u"{}".format(filename[4])
                    audiofile.save()
                elif ext == "mp3":
                    try:
                        shutil.move("D:\Downloads\{}".format(item), "D:\Music")
                    except:
                        none
                elif ext == "mp4":
                    try:
                        shutil.move("D:\Downloads\{}".format(item), "D:\Videos")
                    except:
                        none
                else:
                    if ext not in allfiles:
                        try:
                            os.mkdir("D:\Downloads\{}".format(ext))
                        except:
                            none
                    try:
                        shutil.move("D:\Downloads\{}".format(item), "D:\Downloads\{}".format(ext))
                    except:
                        none
    except:
        none
AMC
  • 2,642
  • 7
  • 13
  • 35
Lucas_HGB
  • 19
  • 2
  • 1
    You can try :close all other programs and then run the code. – Zesty Dragon Sep 22 '20 at 22:37
  • 1
    Why not have the program catch and handle that error? It might be better to check the folder at a regular interval, say ever second or every 5 seconds. You could also look into using windows APIs or other libraries which allow you to watch a directory for changes. – AMC Sep 22 '20 at 23:30
  • 1
    It seems like your program is attempting to rename or remove a file while it is still being written by another program---it makes sense that should fail. Like AMC said, you need to catch and handle the exception. If you haven't seen it yet, this article might help: http://timgolden.me.uk/python/win32_how_do_i/watch_directory_for_changes.html – Pascal Getreuer Sep 22 '20 at 23:42
  • @AMC Just finished rewriting the code with a timer set up to watch for changes in the folder, seems like its working, also added some exception handling and now even when it does get an error for "Permission Denied", it just restarts the process 10 seconds later and tries again. Thank you very much for the help! – Lucas_HGB Sep 23 '20 at 01:37
  • Be careful, using a bare `except` can be dangerous, see https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except. – AMC Sep 23 '20 at 02:17

0 Answers0