Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How can Python3 name the pictures in the source directory with MD5 and set the destination directory?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how Python3 names the pictures in the source directory with MD5 and can set the target directory. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Name the pictures in the source directory with MD5 and set the destination directory

Try to use python to implement a program to generate passwords. It feels easier to use. But the program is still very simple, a Mini Program. This time I need to implement a more complex program, which is to organize pictures.

List requirements

Rename the picture file with the md5 value of the picture.

You can set the source directory

You can set the target directory

You can set whether to move or copy

Parameters can be default

Listing these requirements, I began to design my program.

Finally, the implementation code #! / usr/bin/env python3#-*-coding: UTF-8-*-import sysimport os# acquires the file type library import imghdr# MD5 file operation library import shutil# sets the default parameter sourceDir ='. / 'targetDir ='. / 'operation =' move'images = [] # user operation instructions def Usage (): print ('USAGE: md5img [source dir] [target dir] [- m | -c]') sys.exit (0) # based on the input parameters If len (sys.argv) = = 1 or len (sys.argv) > 4: Usage () if len (sys.argv) > = 2: sourceDir = sys.argv [1] targetDir = sys.argv [1] if len (sys.argv) > = 3: targetDir = sys.argv [2] if len (sys.argv) = 4: if sys.argv [3] ='- if sys.argv: operation = 'move' elif sys. Argv [3] ='- centering: operation = 'copy' else: print (' Operation method is not defined') Usage () # find all the pictures in the source directory def findImage (sourceDir): # if the source directory does not exist Exit if not os.path.exists (sourceDir): print ('Source directory is not defined') Usage () # Files in the target directory of the loop for fil in os.listdir (sourceDir): # get the path to the file filPath = os.path.join (sourceDir) Fil) # determine whether the file is a directory if not os.path.isdir (filPath): # determine whether the file is a picture if imghdr.what (filPath)! = None: # insert the picture into the list to be processed images.append (filPath) # calculate the MD5 value function def calcMD5 (filepath): with open (filepath) 'rb') as f: md5obj = hashlib.md5 () md5obj.update (f.read ()) hash = md5obj.hexdigest () return hash# handles all images def md5img (images): # if the target directory does not exist If you report an error, exit if not os.path.exists (targetDir): print ('Target directory is not defined') Usage () # the list of pictures to be processed in the loop for img in images: # determine the suffix of the picture based on its real suffix If it is jpeg, change it to jpg postfix = 'jpg' if imghdr.what (img) = =' jpeg' else imghdr.what (img) # os.rename (img, targetDir +'/'+ calcMD5 (img) +'.'+ postfix) # perform copy or move operation if operation = = 'move': shutil.move (img TargetDir +'/'+ calcMD5 (img) +'.'+ postfix) elif operation = 'copy': shutil.copy (img, targetDir +' /'+ calcMD5 (img) +'.'+ postfix) # find the picture findImage (sourceDir) # process the picture md5img (images) # complete the print ('Finish') code description

First of all, it refers to all kinds of needed libraries. Sys libraries and os libraries are very basic and commonly used. Then it is for each required function point, find different libraries, and introduce them.

Then, various default parameters are set. Requires a source directory, a target directory, a method of operation, and a list of pictures, an empty array.

Define function

In js, we define a function using function. In python, def is used. I still don't know what def stands for, if you do, tell me in the comment.

Other content, except for format differences, I feel very similar to js.

However, in js, where the function is placed does not affect execution. But it's different in python. The function must be defined before the code being executed. Or you'll get the wrong report.

I haven't coded multiple files yet. If it is to carry out multi-file coding, how to organize the files, I need to think carefully.

User instructions

First of all, I define a function that the user uses. The content is very simple, which is to tell the user how to use this function.

Then, it is the judgment and processing of various terminal input parameters, according to the input parameters, reset my default value, if the parameters are wrong, then need to tell the user that something is wrong.

Ternary operation

There is no ternary operation in python. But there is a similar way to deal with it, but the syntax is different.

For example, in js, the ternary operation is as follows:

Res = a > 5? 0: 1

But there is no ternary operation in python. We can write this:

Res = 0 if a > 5 else 1

It's weird, but by comparison, it can be written.

Determine whether a path exists or not

We can use os.path.exists (path) to determine whether the path path exists.

Find all the files or directories under a path

Os.listdir (path) can list all the files or directories under the path directory. What you get here is an array.

I'm used to js. Python says this is a list.

Determine whether a file is a picture, and if so, the type of picture output

This requires the use of a library, imghdr, which we can refer to at the beginning of the code.

Then we can use imghdr.what (filPath) to see if the file is a picture, and if the output is None, it means it's not a picture. If it is a picture, the type of picture is output. For example, jpg images will output jpeg.

Other summary

The key is to find the corresponding library, and then look at the method of the library. At present, I am not familiar with it, after all, it is just beginning to learn. But through google search, you can quickly find solutions to all kinds of problems.

The only thing to note is that you need to add the keyword python3 to avoid seeing the content of python2. I encountered several pieces of 2 code, which caused the program to fail to run.

The other instructions have been made very clear in my notes.

Python modify picture md5import timewritefile = int (time.time * 1000) with open (path, "a") as f: f.write (str (writefile)) this article on "how Python3 names the images in the source directory with MD5 and can set the target directory" ends here. I hope the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good. Please share it for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report