Python implements traversal of the database and gets the value of the key

Source: Internet
Author: User
Tags file size redis zip

This article to share is the Python implementation to traverse the database and get key value of the method, mainly using for loop to achieve, the need for small partners can refer to.

Traversing the Redis database there are keys stored in the format prefix_* in Key-value mode, and their values are printed.

Traversal uses scan because the keys prefix_* can cause Redis to block for a long time.

Queries use pipeline to reduce interaction and improve efficiency.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23-24 Import Redis import Hiredis pool = Redis. ConnectionPool (host= ' 127.0.0.1 ', port=6379, db=0) R = Redis. Redis (connection_pool=pool) pipe = R.pipeline () pipe_size = 100000 len = 0 key_list = [] for key in R.scan_iter (match= ' prefix_* ', count=100000): Key_list.append (key) Pipe.get (key) If Len < Pipe_size:len + = 1 else:for (k, v) in Zip (key_ List, Pipe.execute ()): Print k, v len = 0 key_list = [] for (K, v) in Zip (Key_list, Pipe.execute ()): Print K, V

Attach the code of other pages, refer to the next bar

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 # filename itertaorfilefolder import os import os.path FilePath = Raw_input (' Enter filePath: ') #遍历文件夹 #三个参数: Returns 1 respectively. Parent Eye Record 2. All folder names (excluding paths) 3. All file names for parent, Dirnames, filenames in Os.walk (FilePath): #输出文件夹信息 for dirname in Dirnames:print ' Parent is: ' +parent print ' dirname are ' + dirname #输出文件信息 for filename in Filenames:print ' parent is: ' +parent print ' fi Lename is: ' + filename #输出文件路径信息 currentpath = os.path.join (parent,filename) print ' The fulll name of the ' file is: ' + cur RentPath filesize = os.path.getsize (currentpath)/1024/1024 print ' The file size is:%.3f MB '% (filesize) #删除大于50m的文件 if f Ilesize > 50:delete = Raw_input (' Are you sure to delete? ') if delete = = ' Yes ': Os.remove (Currentpath)

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.