mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	GH-119169: Slightly speed up os.walk(topdown=True) (GH-121431)
				
					
				
			GH-119186: Slightly speed up `os.walk(topdown=True)` When `os.walk()` traverses into subdirectories in top-down mode, call `os.path.join()` once to add a trailing slash, and use string concatenation thereafter to generate child paths.
This commit is contained in:
		
							parent
							
								
									984d928a22
								
							
						
					
					
						commit
						b19ad11027
					
				
					 2 changed files with 12 additions and 8 deletions
				
			
		
							
								
								
									
										18
									
								
								Lib/os.py
									
										
									
									
									
								
							
							
						
						
									
										18
									
								
								Lib/os.py
									
										
									
									
									
								
							| 
						 | 
					@ -417,14 +417,16 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
 | 
				
			||||||
            # Yield before sub-directory traversal if going top down
 | 
					            # Yield before sub-directory traversal if going top down
 | 
				
			||||||
            yield top, dirs, nondirs
 | 
					            yield top, dirs, nondirs
 | 
				
			||||||
            # Traverse into sub-directories
 | 
					            # Traverse into sub-directories
 | 
				
			||||||
            for dirname in reversed(dirs):
 | 
					            if dirs:
 | 
				
			||||||
                new_path = join(top, dirname)
 | 
					                prefix = join(top, top[:0])  # Add trailing slash
 | 
				
			||||||
                # bpo-23605: os.path.islink() is used instead of caching
 | 
					                for dirname in reversed(dirs):
 | 
				
			||||||
                # entry.is_symlink() result during the loop on os.scandir() because
 | 
					                    new_path = prefix + dirname
 | 
				
			||||||
                # the caller can replace the directory entry during the "yield"
 | 
					                    # bpo-23605: os.path.islink() is used instead of caching
 | 
				
			||||||
                # above.
 | 
					                    # entry.is_symlink() result during the loop on os.scandir() because
 | 
				
			||||||
                if followlinks or not islink(new_path):
 | 
					                    # the caller can replace the directory entry during the "yield"
 | 
				
			||||||
                    stack.append(new_path)
 | 
					                    # above.
 | 
				
			||||||
 | 
					                    if followlinks or not islink(new_path):
 | 
				
			||||||
 | 
					                        stack.append(new_path)
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            # Yield after sub-directory traversal if going bottom up
 | 
					            # Yield after sub-directory traversal if going bottom up
 | 
				
			||||||
            stack.append((top, dirs, nondirs))
 | 
					            stack.append((top, dirs, nondirs))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					Slightly speed up :func:`os.walk` by calling :func:`os.path.join` less
 | 
				
			||||||
 | 
					often.
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue