As a person who is just beginning to learn Python, I try to acquire new knowledge and skills through direct practice. In my opinion, this approach is better than striding guide and multi-volume programming tools.
First of all, I try to cover my needs with projects similar to the one I will tell you about. The second place is occupied by the hope that somebody else may need what I will create (after all the existing errors have been fixed)
The idea of this program came at the moment when I was trying to find a previously uploaded file among the "dumpster" formed in the "Downloads" folder. Images, documents, archives and videos with music: it would be possible to understand this mess and filter out the files, of course, but reluctant because of the time involved. And I thought it would be nice to have a program that would take apart the Downloads folder itself and distribute the files to the standard Pictures, Documents, Videos, and Music folders, and remove the unwanted files. As a result, Perfectionist Organizer was born, and I would like to tell you about it. I would like to say right away that this is an article by a newbie for beginners, so experienced Python programmers won't find anything useful here (except the possibility to look around in the code and point out a lot of errors).
General idea
The first thing the program should do is to determine which operating system it is running in. This is because in Linux and Windows, the "Downloads" folder is located in different ways (I don't have the ability to test the program on a Mac, so I didn't consider this OS). In the detected folder with downloads the program searches all available files and determines what type of files they belong to (music, videos, documents, etc.). After the detected files are moved to default folders, the program finds out from the user what to do with those files that are not suitable for moving - leave or delete them.
Importing the necessary libraries and defining the type of system
To work with the operating system and files in it we need the os library. The getpass library will allow us to find out the user name in the system, and the platform will determine whether it is Windows or Linux. All the necessary libraries are connected at the very beginning via import and after that we define the type of operating system and user name.
Creating a dictionary with popular file extensions and default folders associated with them
With standard Python tools we will create four dictionaries with the most popular file types: music, images, videos and documents.
Don't forget that after the name of the folder there will still be a file name, so at the end I have /. New file types are easily added by editing the dictionary.
Ask the user for the folder name of the download
In Windows and Linux, the download folder is seen in the system with different names. For Windows, this is the Downloads folder regardless of the localization, then on Linux distributions this folder is called "Downloads". In addition, it can happen that the user has changed the name of the folder with the downloaded files and he should be asked if the current directory name differs from the standard one.
The or design allows you to use the default values in the input. That is, if the user does not have any changes in the name of the folder with the downloads, you do not need to do anything, the program itself will substitute the necessary values in the future code.
Set the path to the download folder
Depending on the operating system, the download folder may be in different locations. For Linux, this is the /home/username/ Downloads/ folder, but for Windows it is the C:/Users/username/Downloads/ path. In order not to write these paths manually in the future, it is easier to create variables in which these paths will be specified.
Specify the path to the download folder for a particular user
In order to use the features of the previously connected modules for working with the operating system and files in future, we have to set the path to the folder with the loadings, applying the variables that we created and received earlier.
And also for the convenience of further use of the code we'll set the path of the form /home folder/user.
Check for specific files in the download folder
By searching the dictionary, we compare its keys with the file extensions that are located in the download folder. If the extension corresponds to the key, the necessary file should be moved to the corresponding default folder (the name of which is the value in the dictionary).
Initially, I planned to solve the problem of searching for file extensions by using regular expressions and was engaged in their formulation. But then I read on one of the sites that if you want to solve any problem, you should use regular expressions only last. Python has other simpler and clearer ways to do what you need to do. Therefore, instead of these expressions, the file extension is defined by using the string method ends with. It takes the key from the dictionary and checks whether the file ends with it. Then, using the split() method, only the file name is taken for further moving with os.rename(). All the previously created variables are used as arguments of the latter.
We ask the user what to do with the remaining files
After the necessary files have been sorted, the program must either end or delete those files that were not moved (e.g. exe files or deb packets). The decision about it is made by the user, by default the deletion is not performed.
Summing up
I tried to explain how my program works as clearly as possible. But I think that those who want to make sense of it will still review the code itself several times. Perfectionist Organizer is presented in two versions - console and graphical. You can find both of them by clicking on the link to my Github, as well as for instructions on how to download and use the program. I ran the console version on my main operating system (Archlinux) and the GUI version in a virtual machine on Windows 7.
In both cases, the program worked on "URA". I also want to attach a demonstration of how it works under Linux. If anyone is interested in this topic, the next article will tell you how I did the GUI version and what difficulties I encountered.