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