Python traverses the database and obtains the key value

Source: Internet
Author: User
This article will share with you how to use Python to traverse the database and obtain the key value, mainly through the for loop. for more information, see. Traverse the Redis database with keys stored in key-value mode in the format of PREFIX _ * and print their values.

SCAN is used for traversal, because keys prefix _ * may cause Redis to block for a long time.
Query uses pipeline to reduce interaction and improve efficiency.

import redisimport hiredispool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)r = redis.Redis(connection_pool=pool)pipe = r.pipeline()pipe_size = 100000len = 0key_list = []for key in r.scan_iter(match='PREFIX_*', count=100000):key_list.append(key)pipe.get(key) if len < pipe_size: len += 1else: 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 for other web pages. For more information, see

# Filename itertaorfilefolder import OS. pathfilePath = raw_input ('enter filepath: ') # traverse folders # three parameters: return 1. parent directory 2. all folder names (excluding paths) 3. all file names are for parent, dirnames, and filenames in OS. walk (filePath): # Output Folder Information for dirname in dirnames: print 'parent is: '+ parent print 'dirname is' + dirname # output file information for filename in filenames: print 'parent is: '+ parent print 'filename is:' + filename # output file path information current Path = OS. path. join (parent, filename) print 'the fulll name of the file is: '+ currentPath filesize = OS. path. getsize (currentPath)/1024/1024 print 'The file size is: %. 3f MB '% (filesize) # delete a file larger than 50 MB if filesize> 50: delete = raw_input ('Are you sure to delete? ') If delete = 'yes': OS. remove (currentPath)

The above is all the content of this article. I hope you will like it.

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.