Python script to bring all files from subfolders to main folder


A usual plight with me is bringing all photos I transfer from my phone to one folder. My phone transfer creates a subfolder for each date it has a pic for.

This was till I discovered the module shutil. (No, I knew about it for around _4 months, but I was too lazy to actually write the code 😛 )

This code I have written is meant to be cross-platform. In case of any discrepancies, please do tell.

import shutil
import os

# copy all the files in the subfolders to main folder

# The current working directory
dest_dir = os.getcwd()
# The generator that walks over the folder tree
walker = os.walk(dest_dir)

# the first walk would be the same main directory
# which if processed, is
# redundant
# and raises shutil.Error
# as the file already exists

rem_dirs = walker.next()[1]

for data in walker:
for files in data[2]:
try:
shutil.move(data[0] + os.sep + files, dest_dir)
except shutil.Error:
# still to be on the safe side
continue

# clearing the directories
# from whom we have just removed the files
for dirs in rem_dirs:
shutil.rmtree(dest_dir + os.sep + dirs)


 

Since the code is all documented, I would skip the explanation here.

Please comment, share or like the post. (Good for my writing spirit 🙂 )

Advertisements
Explore posts in the same categories: Python, Windows

Tags: , , , , ,

You can comment below, or link to this permanent URL from your own site.

7 Comments on “Python script to bring all files from subfolders to main folder”

  1. Nate Says:

    This worked like a charm and was exactly what I have been on the search for! Thank you and very well documented

  2. Abraham Iqbal Says:

    Hi, Im sorry, i am a complete noob, im getting this error:

    Traceback (most recent call last):
    File “C:\Users\Administrator\Desktop\Folders\Folder Mover.py”, line 17, in
    rem_dirs = walker.next()[1]
    AttributeError: ‘generator’ object has no attribute ‘next’

    any help would be appreciated 🙂

  3. anon Says:

    Some files may get overridden if they share names, proceed with caution


  4. I would like to move the existing war file to /archieve folder with current date. Below is my function and i am facing the error. Please help me on this ASAP.

    def backupcode(deploymentName,deploymentFile):
    try:

    timestr = time.strftime(“%Y%m%d”)
    warFile = deploymentName.strip() + ‘.war’
    srcPath = deploymentFile + “/” + warFile
    destPath = deploymentFile + “/” + archieve
    listOfFiles = os.listdir(srcPath)
    for f in listOfFiles:
    fullPath = srcPath + “/” + f
    os.system (“mv”+ ” ” + srcPath + ” ” + destPath)
    except:
    print ‘Code Backup FAILED!! ‘
    print dumpStack()

    Error

    Code Backup FAILED!!
    No stack trace available.
    None

  5. Rocco Says:

    This was exactly what I needed when I realized that the script I had to put files into dated directories was working in UTC, but I needed local time. This allowed me to back everything out and start over.

    Perfect!


  6. Hello sir

    I want to do the reverse of what you did.

    I have files like 1,2,3,4,5 and so on placed inside directory “docs”
    Now I want to move these files in a different directory “example” which should contain subfolders 1,2,3,4,…
    And the files should be placed accordingly like subfolder 1 should contain the file1 from the folder “docs” ,file 2 in subfolder 2 and so on.
    Please provide some pointers on how to achieve this using Python script.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: