Shutil - problem with DS_store file blocking movement

Viewed 215

New Python developer trying to work on my first script. The goal is to write an app that cleans up my downloads folder by putting files in subfolders based on file type.

Here's my code:

import shutil
import os

directory = "/Users/Gustaf/downloads"
file_type_list = []
for filename in os.listdir("/Users/Gustaf/downloads"): #insert your downloads folder path
    path = directory
    file_type = os.path.splitext(filename)[1]
    if file_type not in file_type_list:
        file_type_list.append(file_type)
    if file_type in file_type_list:
        continue
    try:
        print(directory + file_type.replace(".", "/"))
        os.mkdir(directory + file_type.replace(".", "/"))
    except OSError as error:
        print(error)

for filename in os.listdir("/Users/Gustaf/downloads"):
    movable_file_path = directory + "/" + "%s" % (filename)
    file_type = os.path.splitext(filename)[1]
    file_type_no_extension = file_type.replace(".", "")
    file_no_extension = os.path.splitext(filename)[0] #used for the full file path in shutil.move
    fileDestination = directory + "/" + "%s" % (file_type_no_extension)

    if os.path.isdir(movable_file_path) == True:
        #skip directories
        print(movable_file_path)
        print("THIS IS A FOLDER" + "\n")
    
    if os.path.isfile(movable_file_path) == True:
        #The files you actually want to move
        print(filename)
        print("THIS IS A FILE" + "\n")
        shutil.move(movable_file_path, fileDestination) 

The first part works, the program creates folders for each file type, but when I try to move files I get this:

Traceback (most recent call last):
  File "/Users/Gustaf/Desktop/Programming/downloads_sorter/main.py", line 41, in <module>
    shutil.move(movable_file_path, fileDestination) 
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/shutil.py", line 786, in move
    raise Error("Destination path '%s' already exists" % real_dst)
shutil.Error: Destination path '/Users/Gustaf/downloads/.DS_Store' already exists

The output from filename, movable_file_path and fileDestination is like this:

DS_Store
/Users/Gustaf/downloads/DS_Store
/Users/Gustaf/downloads/
92722314_10157775412048005_7592678894226898944_n.jpg
/Users/Gustaf/downloads/92722314_10157775412048005_7592678894226898944_n.jpg
/Users/Gustaf/downloads/jpg
epub-download-atomic-habits-by-james-clear-9781847941831-fhy.epub
/Users/Gustaf/downloads/epub-download-atomic-habits-by-james-clear-9781847941831-fhy.epub
/Users/Gustaf/downloads/epub

The first one is the one causing issues (DS_store). Some files are able to move, but after encountering this I get nowhere. What am I doing wrong?

1 Answers

For the following:

  • fileName: DS_Store
  • movable_file_path: /Users/Gustaf/downloads/DS_Store
  • destination: /Users/Gustaf/downloads/

What your code is doing is trying to move DS_Store from /Users/Gustaf/downloads/DS_Store to /Users/Gustaf/downloads/DS_Store which is literally the same place. You aren't trying to move DS_Store at all, so it should be ignored. You can do this by checking to see if the fileName exists in the movable_file_path

In this case, you can just alter your if statement as follows:

if os.path.isfile(movable_file_path) and filename not in movable_file_path:
    #The files you actually want to move
    print(filename)
    print("THIS IS A FILE" + "\n")
    shutil.move(movable_file_path, fileDestination) 
Related