Python: Where Files and Folders Bow to Your Command!

Handling files and folders.


Python is one of the best modern programming languages to automate the regular system management stuff and has several built-in tools or modules for handling files and folders operations. The most common and traditional way for these operations is to use os library, but pathlib is more modern and flexible library for such tasks.

Files and folders operations with Python.
Files and folders operations meme.

Python Knowledge Base: Make coding great again.
- Updated: 2024-12-20 by Andrey BRATUS, Senior Data Analyst.




The pathlib is a Python module which provides an object API for working with files and directories. This module offers classes representing filesystem paths with semantics appropriate for different operating systems. The pathlib is a standard module? you don't need to install it.


  1. Files search in your competer:


  2. 
    from pathlib import Path
    
    root_dir = Path('.')
    search_term = 'bratus'
    
    for path in root_dir.rglob("*"):
      if path.is_file():#remove this if if you also need folders search
        if search_term in path.stem:
          print(path.absolute())   
    


    OUT: /home/andrey/bratus.html


  3. Creating a bunch of empty files:


  4. 
    from pathlib import Path
    
    root_dir = Path('newfiles')
    
    for i in range(1, 24):
      filename = str(i) + '.csv'
      filepath = root_dir / Path(filename)
      filepath.touch()
    
    print('files were created !!!')    
    

    OUT: files were created !!!


  5. Deleting the files:


  6. 
    from pathlib import Path
    
    root_dir = Path('destination')
    
    for path in root_dir.glob("*.html"):
      with open(path, 'wb') as file:
        file.write(b'')#empties the file
      path.unlink()#deletes the file
    print("files are deleted completely !!!")  
    

    OUT: files are deleted completely !!!



  7. Renaming all files in a folder (add prefix):


  8. 
    from pathlib import Path
    
    root_dir = Path('filesfolder')
    file_paths = root_dir.iterdir()
    # print(Path.cwd())
    for path in file_paths:
      new_filename = "my-" + path.stem + path.suffix
      new_filepath = path.with_name(new_filename)
      # print(new_filepath)
      path.rename(new_filepath)
    
    print('All files were renamed !!!')  
    

    OUT: All files were renamed !!!


  9. Renaming files using folder name:


  10. 
    from pathlib import Path
    
    root_dir = Path('myfiles')
    file_paths = root_dir.glob("**/*")
    
    for path in file_paths:
      if path.is_file():
        parent_folder = path.parts[-2]
        new_filename = parent_folder + '-' + path.name
        # print(new_filename)
        new_filepath = path.with_name(new_filename)
        path.rename(new_filepath)
    
    print('All files were renamed !!!') 
    

    OUT: All files were renamed !!!


  11. Adding file created dates to filenames in a folder:


  12. 
    from pathlib import Path
    from datetime import datetime
    
    root_dir = Path('myfiles')
    
    for path in root_dir.glob("**/*"):
      if path.is_file():
        created_date = datetime.fromtimestamp(path.stat().st_ctime)
        created_date_str = created_date.strftime("%Y-%m-%d_%H:%M:%S")
        new_filename = created_date_str + '_' + path.name
        new_filepath = path.with_name(new_filename)
        path.rename(new_filepath)
    
    print('File created dates were added to filenames !!!') 
    

    OUT: File created dates were added to filenames !!!


  13. Changing files extensions to all files in a folder:


  14. 
    from pathlib import Path
    
    root_dir = Path('myfiles')
    
    for path in root_dir.rglob("*.csv"):
      if path.is_file():
        new_filepath = path.with_suffix(".txt")
        path.rename(new_filepath)
        
    print('Files extensions were changed !!!')
    

    OUT: Files extensions were changed !!!


  15. Creating zip archive for files:


  16. 
    from pathlib import Path 
    import zipfile
    
    root_dir = Path('files')
    archive_path = root_dir / Path('myarchive.zip')
    
    with zipfile.ZipFile(archive_path, 'w') as zf:
      for path in root_dir.rglob("*.csv"):
        zf.write(path)
        # unlink if original files to be removed
        # path.unlink()
    
    print('Archive is created !!!')
    

    OUT: Archive is created !!!


  17. Unzip the files:


  18. 
    from pathlib import Path
    import zipfile
    
    root_dir = Path('.')
    destination_path = Path('destination')
    
    for path in root_dir.glob("*.zip"):
      with zipfile.ZipFile(path, 'r') as zf:
        final_path = destination_path / Path(path.stem)
        zf.extractall(path=final_path)
    
    print('Files are unzipped !!!') 
    

    OUT: Files are unzipped !!!





See also related topics: