Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move files in folders to a top-level directory

I am trying to complete a script for my work that cleans up their file organization system. The last part of my script needs to go inside all folders in a given directory, and move all files in every folder to the directory. For example:

import os
path = 'C:/User/Tom/Documents'
folders = os.listdir(path)
print(folders)

Lets say the folder structure is like this:

Documents  
[________ Folder A    
..................[_________ File 1  
..................[_________ File 2  
[________ Folder B  
..................[_________ File 3  
..................[_________ File 4  
[________ Folder C  
..................[_________ File 5  
..................[_________ File 6  
..................[_________ File 7  

My goal is to somehow go into each folder under "Documents", empty out the folders by moving all of the files one level up without having to input folder names or file names to be in the Documents path that looks like this:

Documents  
[________ Folder A    
[_________ File 1  
[_________ File 2  
[________ Folder B  
[_________ File 3  
[_________ File 4  
[________ Folder C  
[_________ File 5  
[_________ File 6  
[_________ File 7  

I am newer to python and my only thought on how to efficiently do this would be to type out a lot of code that goes into each folder directory, and shutil.move() them. However this wouldn't work for my application as the script needs to be able to complete this task for X amount of folders with Y amount of files and not having to input each folder path.

I am asking for any advice on an efficient way I can loop through my 'folders' list and simply move the files out of the folder into the path's directory.
Sorry for the long post, I just wanted to be as detailed as possible about my question, thanks!

like image 395
Bkal05 Avatar asked Aug 16 '17 01:08

Bkal05


People also ask

How do I move files up one level?

Similarly, you can move a file or folder up in the hierarchy and out of the current folder it resides in by dragging your file or folder up to the top of the list and looking for the indented gray box underneath the "Up One Level" link. Dropping a file or folder there will move it up one level.

How do I move a file to a higher directory in Linux?

You need to use the mv command that moves one or more files or directories from one place to another. You must have have write permission for the directories which the file will move between. The syntax is as follows to move /home/apache2/www/html directory up one level at /home/apache2/www/ directory.

What does top-level folder mean?

The top-level folder or top-level directory (same thing) is a reference to the root level of a project. So if you have a project structure like this: your-project > Components > Header > Footer package.json README.md. The top-level directory is your-project and everything inside it are top-level files & folders.


1 Answers

I would recommend a recursive bottom-up traversal using os.walk, and moving your files accordingly.

import os
import shutil
doc_path = 'C:/User/Tom/Documents'

for root, dirs, files in os.walk(doc_path, topdown=False):
    for file in files:
        try:
            shutil.move(os.path.join(root, file), doc_path)
        except OSError:
            pass

This will move everything to the top level directory.

like image 165
cs95 Avatar answered Oct 21 '22 06:10

cs95