sangharsh
sangharsh

Reputation: 158

Python code for sorting and moving files by types

My code is working fine to serve its objective but there is a small bug which I can not over ride. It is about "makedir" command in python code.

I have lots of files in my downloads section. So thought of sorting them as per their files types to organize the folder. The project includes -

  1. Creating directories for storing different file types according to their types.

  2. Identifying and moving files to respective directories according to their types. I have return a code mentioned below.

import os, glob, shutil

os.chdir('/home/something/Downloads')
src_dir = os.path.join('/home/something/Downloads')
try:
    os.makedirs('/home/something/Downloads/excel')
    os.makedirs('/home/something/Downloads/image')
    os.makedirs('/home/something/Downloads/pdf')
    os.makedirs('/home/something/Downloads/word')
    os.makedirs('/home/something/Downloads/python')
    os.makedirs('/home/something/Downloads/text')
    os.makedirs('/home/something/Downloads/gimp')
    os.makedirs('/home/something/Downloads/video')
    os.makedirs('/home/something/Downloads/presentation')
    os.makedirs('/home/something/Downloads/zip')

except FileExistsError:
    pass

pdfDir = (src_dir + '/pdf')
txtDir = src_dir + '/text'
pyDir = src_dir + '/python'
docDir = src_dir + '/word'
gimpDir = src_dir + '/gimp'
imgDir = src_dir + '/image'
avDir = src_dir + '/video'
pptxDir = src_dir + '/presentation'
zipDir = src_dir + '/zip'
excelDir = src_dir + '/excel'

pdfFiles = glob.glob(src_dir + '/*.pdf')
txtFiles= glob.glob(src_dir + '/*.txt')
pyFiles = glob.glob(src_dir + '/*.py')
docFiles = (glob.glob(src_dir + '/*.docx') + glob.glob(src_dir + '/*.docs') + glob.glob(src_dir + '/*doc') + glob.glob(src_dir + '/*.odt') + glob.glob(src_dir + '/*.DOC'))
gimpFiles = glob.glob(src_dir + '/*.svg') + glob.glob(src_dir + '/*.xcf')
imgFiles = glob.glob(src_dir + '/*.img') + glob.glob(src_dir + '/*.png') + (glob.glob(src_dir + '/*.jpg') + glob.glob(src_dir + '/*.jpeg')+ glob.glob(src_dir +'/*.JPG'))
avFiles = glob.glob(src_dir + '/*.mp4')
pptxFiles = glob.glob(src_dir + '/*.pptx')
zipFiles = glob.glob(src_dir + '/*.zip')
excelFiles = glob.glob(src_dir + '/*.xlxs') + glob.glob(src_dir + '/*.xlsx') + glob.glob(src_dir + '/*.ods')

for files in pdfFiles:
    shutil.copy(files, pdfDir)
for files in pyFiles:
    shutil.copy(files, pyDir)
for files in txtFiles:
    shutil.copy(files, txtDir)
for files in gimpFiles:
    shutil.copy(files, gimpDir)
for files in docFiles:
    shutil.copy(files, docDir)
for files in imgFiles:
    shutil.copy(files, imgDir)
for files in avFiles:
    shutil.copy(files, avDir)
for files in pptxFiles:
    shutil.copy(files, pptxDir)
for files in zipFiles:
    shutil.copy(files, zipDir)
for files in excelFiles:
    shutil.copy(files, excelDir) 

It works very fine and serves the purpose except one difficulty. If "makedirs" command encounters with a file name already existing in Directory "Downloads", it pass the command and dose not create new directory. For instance if file name "image" is already existing in the directory "Downloads" then, "makedirs" command fails to create new directory with same name and so the the image files types are not sorted accordingly.

I am not sure how to resolve this bug as there is no "directoryExistsError" as "FileExistsError"!

Kindly guide.

Upvotes: 0

Views: 557

Answers (2)

Vítor Cézar
Vítor Cézar

Reputation: 269

It is not possible to create a file and a folder with the same name on the same directory. The operating system does not permit it, so it is impossible for Python as well.

Upvotes: 1

Jonatan Öström
Jonatan Öström

Reputation: 2629

You can not have a file and a directory both with the same name. So you have to rename or move the file before you create the directory.

Upvotes: 1

Related Questions