This is a script to automate cleanups in any directory. It will move files into folders according to the file extension.
Why?
A clean home/project directory allows us to be able to work productively, without spending much time searching for the particular document/file we need.
This is a simple script to help you clean your directory in seconds
Caution
DO NOT run the script on any directory containing system/important files.
This is a python script, we will be using modules os and shutil. First of all, create a new script called script.py, and import the modules required for this
1
2
3
4
#!/usr/bin/env pythonimportosimportshutil
Notice the first line #!/usr/bin/env python. This is called a shebang. It allows us to run the script as an executable, with the command
1
./script.py
Without the shebang, we will need to use the full command
1
python3 script.py
Defining Main
This step is optional, although it will make the code look cleaner
This is how I usually start my code, you will be able to run your code now with the command
1
2
./script.py
Output: Hello,World!
Defining a dictionary
A dictionary has a key, value pair, similar to a JSON file structure. In this script, we will create a dictionary of File type(Key) and File Extensions (Value).
file_moved_counter=0file_list=os.listdir('.')forfileinfile_list:if'directory_cleanup'notinfileandfilenotinDIRECTORIES:# Check if file is a folder firstifos.path.isdir(file):shutil.move(file,'./Folder/{}'.format(file))file_moved_counter+=1else:print('Current File: '+file+'\n')filename,file_extension=os.path.splitext(file)fordirinDIRECTORIES_DICTIONARY:iffile_extensioninDIRECTORIES_DICTIONARY[dir]:os.rename(file,'./'+dir+'/'+file)print('MOVED FILE: '+file+'\n')file_moved_counter+=1#Moving files with unknown file extensions to Others foldernew_file_list=os.listdir('.')forfileinnew_file_list:if'directory_cleanup'notinfileandfilenotinDIRECTORIES:os.rename(file,'./Others/'+file)file_moved_counter+=1print('Files Organized!\nTotal file moved: '+str(file_moved_counter))
These lines will move files to their folder based on their file extensions, and move undefined extensions to the ‘Others’ directory.
In this script, I’ve used both modules os and shutil to move files to show the difference in syntax for each of the modules.
Running the script
1
./script.py
To test the script, you should add/create files with any extensions in the same directory as your script. Upon running the script, you should see all your files moved and categorized according to their file extensions.
Tips
I usually create a project folder for any scripts that I create, and run the script in the folder. This prevents any unwanted modifications to any directory.
Note that indentations in a python script is very important.
This script is useful in certain directories, like Documents or Downloads, as it categorizes those files accordingly and cleans the directory.
Summary
Thats it! You have yourself a cleanup script that you can use to clean any directory you want! The full code is below, do take a look if you are facing any problems!