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

Python can only specify file types

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "Python can only specify file types". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

How to judge whether the USB disk is inserted or not?

First of all, we open the computer terminal, enter the / Volumes directory, and insert the USB disk at this time, we can find that it is mounted under this directory, that is, we just need to scan the directory at a fixed time, and when a new folder appears in this directory, it is likely that a USB drive has been inserted.

My design is to use the time.sleep (3) function to keep the program running and check the / Volumes/ directory every three seconds, and if there are extra folders, copy it to another folder.

As the title shows, we really completed the "virus" in only 10 lines (actually 11 lines). We can find all the directories in usb lying in the home directory half a minute after insertion.

How to selectively copy files?

We just wrote a very simple script to test the feasibility of this idea, but there is still a problem. Just now, I was able to copy all the files in the U disk very quickly, because there are only two or three files in the U disk, and the size is no more than 15m. If there are a lot of movies, music, files that we don't need on the target U disk, our program should be able to skip them and choose only important files such as .docx such as .ppt files, or just copy those files that have been recently modified, or exclude all files larger than 5m in size. Can we do that with python? That's for sure!

All files in the os.walk recursive folder

Let me give you an example.

I created a testwalk folder under a certain directory, which contains three files of file123.txt and three folders of folder123, among which there are files file4.txt and folder4 in folder1.

Now let's test it.

Root stores the current location. It will search all the folders under. / testwalk/ as the root directory.

View dirs separately

View files separately

Okay, now we need to recurse the usb folder, find all the file, check the size, if it's less than, say, 3m, copy it into home, and drop it if it's greater than that.

Shutil module

Now let's take the folder just now as an example. If you want to copy file1.txt to folder2:

There are many other tools that can be used in shutil, which are not detailed here.

Os.path.getsize () determines the size

Os.path.getsize (filename) returns a value in byte. To view the file size, we need to manually write a function to convert it into an easy-to-read form.

Here we just need to select a file size smaller than 3m, 3M = 3 * 1024kB = 3 * 1024*1024byte

Combined with shutil.copy2, files of the selected size can be copied to our target folder.

How to specify a file type

Regular expressions are needed to help us here.

As follows, we let the specified file suffix and the specified file size be copied into our target file:

Don't forget to import re

File types can be better specified with more complex regular expressions

Filter files based on modification time

At this point, I created a file in the directory called newfile.

So much for the content of "Python can only specify file types". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report