[Python] File Sorter 06-19-2014, 08:24 AM
#1
Was getting bored and my D Drive was awfully filled with all types of junk files so I decided to sort em up, this quick script I coded does all the work, So far I'm happy with it 
Before:
![[Image: ifydjB6.png]](http://i.imgur.com/ifydjB6.png)
After:
![[Image: hBD6amU.png]](http://i.imgur.com/hBD6amU.png)
Source Code:
How to Run?
The script name is followed by the directory where you want to sort things up, For example I want to sort files in D:/Software so
Note You can add more extensions dictionary, For example to add .mp3 do:
This project is also on Github so you can view it there too https://github.com/Ex094/File-Sorter
Hope this helps
For more releases follow me up on my github
Regards,
Ex094

Before:
![[Image: ifydjB6.png]](http://i.imgur.com/ifydjB6.png)
After:
![[Image: hBD6amU.png]](http://i.imgur.com/hBD6amU.png)
Source Code:
Code:
import os, shutil, sys
###Extensions and their respective folder names
extension = {'Text': '.txt', 'Executables': '.exe', 'Python Executable': '.py'}
def file_sort(path):
###Path where the script is located
files = os.listdir(path)
###Check if folder is not empty
if (files == 0):
print "No files found in this directory"
else:
for ex in extension:
for item in files:
if item.endswith(extension[ex]):
try:
###If Folder exists then move file
if os.path.isdir(path + ex):
shutil.move(path + item, path + ex)
###Else create folder then move file
else:
os.makedirs(path + ex)
shutil.move(path + item, path + ex)
###Error handler in case if files already exist
except:
print "Files And Folders have been sorted already!"
break
try:
if(sys.argv[1]):
if(os.path.isdir(sys.argv[1])):
print "Sorting files in defined path: " + sys.argv[1]
file_sort(sys.argv[1])
else:
print "Directory does not exist"
except:
print "No Directory or Path Defined!"
print "Command: filesort.py C:/folder/folder"How to Run?
The script name is followed by the directory where you want to sort things up, For example I want to sort files in D:/Software so
Code:
filesort.py D:/Softwares/Note You can add more extensions dictionary, For example to add .mp3 do:
Code:
extension = {'Text': '.txt', 'Executables': '.exe', 'Python Executable': '.py', 'MP3': '.mp3'}This project is also on Github so you can view it there too https://github.com/Ex094/File-Sorter
Hope this helps
For more releases follow me up on my githubRegards,
Ex094




![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: qJweLN6.jpg]](http://i.imgur.com/qJweLN6.jpg)
I didn't see that lol