Best practice: working with path in Python


Same problem: list of folders and drives

In the last article, we used a recursive function of less than 10 lines to solve the problem of scanning folders and ranking files by modified date and size.





Now I will raise the bar and show you how you could have done better.





Pathlib

?





:





path_file = os.sep.join([path_dir, filename])
      
      



, , «+» .





, , , .





path_dir: str = r"C:/Users/sselt/Documents/blog_demo/"  # abschließender Trenner
filename: str = "some_file"
path_file = os.sep.join([path_dir, filename])
# C:/Users/sselt/Documents/blog_demo/\some_file
      
      



, , . , , .





Python 3.4 — pathlib



. os - .





, :





import os
path = "C:/Users/sselt/Documents/blog_demo/"
os.path.isdir(path)
os.path.isfile(path)
os.path.getsize(path)
      
      



:





from pathlib import Path
path: Path = Path("C:/Users/sselt/Documents/blog_demo/")
path.is_dir()
path.is_file()
path.stat().st_size
      
      



. ?





-





-, , . , path



, .





:





filename: Path = Path("some_file.txt")
path: Path = Path("C:/Users/sselt/Documents/blog_demo")
print( path / filename )
# C:\Users\sselt\Documents\blog_demo\some_file.txt
      
      



. path



, . 





path



:





filename: Path = Path("some_file.txt")
# hier path mit überflüssigem Trenner am Schluss
path: Path = Path("C:/Users/sselt/Documents/blog_demo/")
# hier path mit doppeltem Trenner
path: Path = Path("C:/Users/sselt/Documents/blog_demo//")
# hier path völlig durcheinander
path: Path = Path("C:\\Users/sselt\\Documents/blog_demo")  # hier ein wilder Mix
# alle Varianten führen zum selben Ergebnis
print(path/filename)
# C:\Users\sselt\Documents\blog_demo\some_file.txt
      
      



, . . generic path



, Windows WindowsPath



, Linux PosixPath







, , . str(Path)



.





os.walk

os.listdir



, os.path.isdir



.





os.walk



. , , . . , .





os.walk Pathlib

, , , , :





filesurvey = []
for row in os.walk(path):   # row beinhaltet jeweils einen Ordnerinhalt
    for filename in row[2]:  # row[2] ist ein tupel aus Dateinamen
        full_path: Path = Path(row[0]) / Path(filename)   # row[0] ist der Ordnerpfad
        filesurvey.append([path, filename, full_path.stat().st_mtime, full_path.stat().st_size])
      
      



, . !





.






«Python Developer. Basic».









- « : map(), filter() zip()».





, , ? , , Python? "callback", "iterator" "lambda". — !












All Articles