Share two useful Python gadgets

Hello, everyone, today I would like to introduce to you two small tools that are easy to use in Python.

Although the usage is simple, it can greatly improve the development efficiency.

1. tqdm

When the forloop in the program takes a lot of time to execute, we often add an 计数器output program execution progress.

The disadvantage of this method is that it needs to write some code that is not related to the business, and it prints too much information.

If we use tqdma progress bar to display the execution progress of the program, the above shortcomings can be effectively avoided.

picture

Very simple to use

import time
from tqdm import tqdm
for i in tqdm(range(100)):
    time.sleep(1)

This gadget also has other useful functions, you can explore it yourself if you are interested.

2. glob

The second gadget is a module that comes with the Python library -- glob.

It can Unixaccess local files in style.

For example, we want to list all giffiles in the current directory

import glob

glob.glob('./*.ipynb')

The output is as follows:

['./cnn.ipynb', './blob.ipynb', './test.ipynb']

and many more examples

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True)
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
['./', './sub/']

How, is the gadget simple and efficient, haha, I hope it will be useful to everyone.

recommended article

Technology Exchange

Welcome to reprint, collect, like and support!

insert image description here

At present, a technical exchange group has been opened, and the group has more than 2,000 members . The best way to remark when adding is: source + interest direction, which is convenient to find like-minded friends

  • Method 1. Send the following picture to WeChat, long press to identify, and reply in the background: add group;
  • Method ②, add micro-signal: dkl88191 , note: from CSDN
  • Method ③, WeChat search public account: Python learning and data mining , background reply: add group

long press follow

Guess you like

Origin blog.csdn.net/qq_34160248/article/details/124344614