In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "use python to organize mobile phone picture mini application", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "using python to sort out mobile phone picture mini app"!
cause
A few days ago, I went to Guotu to shoot a book, a book on psychology. Maybe you asked me why I didn't buy one or look for pdf on the Internet. In fact, books on psychology can be said to be a pile of books on the market. As for the two reasons for shooting this book, one is that there is no collection value that is not worth buying, and it is only suitable for emergency use. Then, the writing characteristics of the author of this book are different from those of other members of the public, which can be said to be characteristic or partial, so I took a bunch of pictures on my mobile phone and later sorted them into pdf. But when I looked at it yesterday, the original picture file was almost thousands. It's annoying to choose to delete one by one.
Perhaps you will say why not import to the computer to delete, there is no way I just want to be different, learned python is like the whole point of coquettish operation. (manual funny)
So let's use python to do a lot of operations on the mobile phone.
Prepare the tool
First of all, you have to have a phone, 233. seriously, since I'm going to run python on my phone, I recommend pydroid3 here, because the code is based on python3.6 (use of f-string).
In order to facilitate you to download, I provide you with Baidu Cloud download address: background reply: pydroid.
After the installation is opened, it looks like this.
All that's left is to run the code, which we'll talk about later.
Train of thought
We need to know exactly how to do it before we do it.
1. Determine the directory where the picture file to be processed is located
two。 Then determine which images to delete, and here we filter based on the date the image was created.
3. To prevent accidental deletion, create a trash directory in this folder for security reasons, first move the target image to the folder, and then confirm that the image can be deleted before deleting it. Just like don't use rm easily, you know. Give yourself time to go back on your promises.
Code implementation
Well, now that we've decided on the idea, we can do it.
1. Confirm the folder where the file is located, where the pathlib module is used. Pathlib is a standard library in python3, mainly the operation path, which is different from the operation path of the os module, that is, the splicing path is sometimes more convenient, and the best thing is to use the two together. All right, here's the code.
Import pathlib
Dir_path = pathlib.Path (filename). Resolve (). Parent
The resolve method is to get the directory where the file is running, and parent is to get the directory above, that is, the folder where the picture is located.
two。 Create the trash directory and move the image to the directory where the image is located.
It is mentioned in the thought analysis that the function of this folder is similar to a dustbin that can be confirmed and then deleted.
Trash_dir = dir_path.joinpath ("trash")
We can use the joinpath method to stitch the path. Os.path.join () needs to fill in two parameters, and only one is needed here, which is the advantage. But it should be noted that the final path obtained by this method is an object, not a string.
A path that needs to be converted to a string in later use.
3. Next we know that the file type we need to operate is pictures, here I take jpg as an example, of course, if your picture is png, you can write png,python3 's standard library provides glob module, this module can set matching rules to get the list of pictures to match.
Import glob
Image_path_list = glob.glob (to_str_path (dir_path.joinpath ("* .jpg")
It was said earlier that the acquired path is an object, so to_str_path is a method that converts to a string path.
* .jpg can match the list of production results of files with the suffix jpg.
4. All the files have been obtained, but they are not necessarily what we need, the documents within the specified date we need.
Import os
File_time = os.path.getctime (image_path)
Through getctime, we obtained the creation time of the image for further screening.
5. Once the files are ready, we need to move to the trash directory and wait for processing.
Files can be moved through the rename method of the os module or the move method of shutils. Here I choose the os module.
Import os
Os.rename ("full file path before moving", "full file path after moving")
6. The files are now in the trash directory.
Then you can delete it after confirmation, and execute the delete method if there is no problem.
There are two ways to delete, one is to delete all files, the other is to delete the entire folder, the only difference between the two is that you don't have to create a trash directory next time, there's not much difference.
If del_type = = "dir":
Shutil.rmtree (trash_path) # Recursively delete a directory and all the contents in the directory
Else:
If os.path.exists (trash_path):
File_list = os.listdir (trash_path)
If file_list:
Remove_f = partial (remove, trash_path=trash_path)
Start_thread (remove_f, file_list)
Demo
First of all, determine the directory in which the files need to be sorted out.
Here take my previous mobile phone screenshot directory as an example. There is a small problem here is, how do I know which directory my screenshot files are in? .
Generally speaking, when a mobile phone looks at a picture, there is a detailed information which usually has a path. Go down like this.
Through the picture, we can clearly see the path in the screenshot folder, so we have determined the directory of the file.
Then to make it easier for us to execute the python script in the directory where we need to operate, open the pydroid3 that we installed earlier
And then follow my actions.
1. Copy the code into the box below
The picture is too big to demonstrate the whole process. Look at the picture of the blog park.
You can change newfile to another name.
two。 Call the method to pass in parameters
Method passes in a date parameter format such as 20190101.
Here, take 20171227 as an example to run the code.
There was no result after running, and then I took a look at the picture and learned that the default picture format of the program is jpg, and my screenshots are all png, so I need to specify the filetype parameter to png, and the date_str parameter to set to 20171227.
Run the code:
You will find that the specified file is already in the trash directory.
3. You can then delete it by executing the delete method. Conclusion
At this point, this is the end of the tutorial, originally intended to be written as a multi-threaded version of the speed up, but found that the phone does not seem to respond, so it is best to use loop operation files, here is just to provide such an idea, the code is not difficult. The key is to see the use, how do you learn.
Thank you for your reading, the above is the content of "finishing Mobile Picture Mini App with python". After the study of this article, I believe you have a deeper understanding of the problem of sorting out Mobile Picture Mini App with python, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.