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.