bkrause404
bkrause404

Reputation: 19

Moving folders to other folders with python

I'm currently working on a program that will clean up my desktop by moving everything into corresponding folders that are within another folder. For example, when you run the script, it makes a folder on your desktop called "desktop items" and then within that folder are subfolders such as "documents" "applications" "code", etc. Right now I have the individual files on my desktop being stored correctly, but I'm not sure how I would handle the folders. I just want to store the already made folders within the "desktop items" folder. Thanks!

Upvotes: 0

Views: 59

Answers (1)

Carles Mitjans
Carles Mitjans

Reputation: 4866

You can make use of shutil module. The function shutil.move(src, dst) will do the trick.

Where src will be the already made folder and dst will be the folder "desktop items".

Edited: For an arbitrary number of folders you can use module os like this:

for elem in os.list("desktop_path"):
    if os.path.isdir(os.path.join("desktop_path", elem)):
        shutil.move(os.path.join("desktop_path", elem), "desktop items folder")

Upvotes: 1

Related Questions