![]() |
|
[Python] File Sorter - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Python (https://sinister.ly/Forum-Python) +--- Thread: [Python] File Sorter (/Thread-Python-File-Sorter) Pages:
1
2
|
[Python] File Sorter - Ex094 - 06-19-2014 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: ![]() After: ![]() 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 RE: [Python] File Sorter - L0aD1nG - 06-19-2014 You could make it more handy man by making it accept the path you want to sort as args. So you dont have to put it in the folder you wanna sort...i guess you know how..? RE: [Python] File Sorter - Anima Templi - 06-19-2014 I actually made a similar tool a while ago. https://github.com/AnimaTempli/Organizer RE: [Python] File Sorter - L0aD1nG - 06-19-2014 You could grab the whole code to a function and do this.. Code: import optparse
import sys
def my_sweet_sort(path):
<the whole code>
def main():
parser = optparse.OptionParser("Usage : " + sys.argv[0] + " -p <path you would like sort>" )
parser.add_option('-p', dest = 'sort_path', type = 'string', help = 'This is the path location you want to sort')
(options, args) = parser.parse_args()
sort_path = options.sort_path
if not sort_path:
print parser.usage
exit(1)
else:
my_sweet_sort(sort_path)
exit(0)
## At last check if debug mode given and run it
if __name__ == '__main__':
main()So you can now do this from the cmd: python myscript.py -p <path to sort> I just made a suggestion i hope that is allowed ![]() Well done posting the code so everyone can learn from i love people like you! Also well done doing that with python!!! Cheers! RE: [Python] File Sorter - Ex094 - 06-19-2014 @L0aD1nG I like the suggestion, I'll see what I can do AND YES SUGGESTION ARE ALLOWED lol Every one needs feedback so that they can improve. BTW Welcome to community ![]() @Anima Templi Nice I didn't see that lol
RE: [Python] File Sorter - L0aD1nG - 06-19-2014 (06-19-2014, 12:50 PM)Ex094 Wrote: @L0aD1nG I like the suggestion, I'll see what I can do Thank you man i was registered but inactive i was studding a new book and making a nice little program now that i am finished i will be active.. RE: [Python] File Sorter - Anima Templi - 06-19-2014 You want feedback? Well, take a look at mine, lol. Maybe add a config file like I did, to easily add new file types/folders. Also, as already suggested, make it take arguments from the command line. This will make it much easier for the user, to actually choose a path. (06-19-2014, 12:19 PM)L0aD1nG Wrote: You could grab the whole code to a function and do this.. I really won't recommend this code, sorry. Optparse has been deprecated since of Python 2.7 besides, argparse has several more features than optparse.
Second, it's not really recommended to create a main function like that in Python. Absolutely no need for it. And "my_sweet_sort() ? Function/class and variable names should tell as much about the function/value they hold. I hope you don't take this the wrong way, but since you gave feedback, I thought it was ok for me to do the same thing. RE: [Python] File Sorter - coolshame - 06-19-2014 Nice, but it simply use Right Click and Sort by> any! RE: [Python] File Sorter - Ex094 - 06-19-2014 (06-19-2014, 01:54 PM)coolshame Wrote: Nice, but it simply use Right Click and Sort by> any! Run the program then re comment It organizes your files by making respective folders. Why would I made a program to sort file by names or size
RE: [Python] File Sorter - L0aD1nG - 06-19-2014 (06-19-2014, 01:39 PM)Anima Templi Wrote: You want feedback? Well, take a look at mine, lol. Maybe add a config file like I did, to easily add new file types/folders. Also, as already suggested, make it take arguments from the command line. This will make it much easier for the user, to actually choose a path. First of all i havent tried that argparse module but i think that same can happen with optparse..but i am not sure as i havent check the argparse module yet. On the other hand now i didnt recommend him naming functions like that i just wanted to show him the way that optparse works so i just putted a random and little funny name to the function.Also using the main funtion is highly recommended by pro pythonistaz cause its a way to nicely organize all the code at last and run it like that after check if not debuging flag is on. |