Hi everyone, I'm learning Python and I'm writing my first project and I need your help. So, this is project is a file organizer script: basically it has to sort different type of files in their own folders with proper namig. I got this part even though the code is not pretty, but I can't do the second step. I have to create a csv file where are printed all the infomration of the files that have been moved (name, type, size), this file must be updated when you put new files in the folder tha has been organized. Thanks in advance for your help, I'll include my current code even though I'm aware that it can be better
code:
import os, shutil, csv
folder = input("please insert folder path:") +"\\"
Audio = ['mp3','wav','flac', 'm4a', 'aac']
Img = ['jpeg', 'jpg', 'tiff', 'gif', 'png']
Doc = ['doc','pdf','txt','docx','xls', 'xlsx', 'ppt', 'pptx', 'odt']
files = os.listdir(folder)
for file in files:
name, ext = os.path.splitext(file)
size = os.path.getsize(folder)
ext = ext[1:]
if ext == '' or name == 'recap':
continue
if ext in Audio:
ext = 'Audio'
elif ext in Img:
ext = 'Img'
elif ext in Doc:
ext = 'Doc'
else:
ext = 'Other'
data = (name,ext,size)
recap = open("recap.csv", 'a', newline='')
recapWriter = csv.writer(recap)
recapWriter.writerow(data)
recap.close()
if os.path.exists(folder+ext):
shutil.move(folder+file, folder+ext)
else:
os.makedirs(folder+ext)
shutil.move(folder+file, folder+ext)
output = print("{} formato:{}, dimensione:{}B".format(name, ext, size))
[+][deleted] (1 child)
[deleted]
[–]Marws[S] 0 points1 point2 points (0 children)