<jupyter_start><jupyter_text>File System Manipulation With PythonBy <NAME>1. Rename files to add padding to sequentially-numbered files---  1. Rename files to add padding to sequentially-numbered filesI really hate when sequentially-numbered files are not padded with additional zeros. Some programs / OS's will sort them correctly without the padding, some will not. Here is an example:```517K Jan 30 14:35 1.png824K Jan 30 14:35 10.png582K Jan 30 14:35 11.png514K Jan 30 14:35 12.png...500K Jan 30 14:35 2.png613K Jan 30 14:35 20.png674K Jan 30 14:35 21.png687K Jan 30 14:35 22.png```I find that really annoying. And it's an easy fix using Python...<jupyter_code># === Imports === #
import os
# === Loop to rename all files under root === #
# Set up working dir
root_dir = "/Users/Tobias/workshop/buildbox/neurecycle/assets_/waste_images/trashpanda_v1"

# First loop through all directories
for subdir in os.listdir(root_dir):
    os.chdir(root_dir)  # Move (back) to root
    os.chdir(subdir)  # Move to working dir
    print(os.getcwd())
    # Then loop through all files within the dir
    for f in os.listdir():
        f_name, f_ext = os.path.splitext(f)
        f_new_num = f_name.zfill(3)
        f_new_name = f"{f_new_num}{f_ext}"

        print(f"\t {f_new_name}")

        # Rename the file, being sure to use path/filename
        os.rename(f, f_new_name)
# === Loop to rename all files under root === #
# Set up working dir
root_dir = "/Users/Tobias/workshop/buildbox/neurecycle/image_data"

for f in os.listdir():
    f_name, f_ext = os.path.splitext(f)
    f_new_num = f_name.zfill(3)
    f_new_name = f"{f_new_num}{f_ext}"

    print(f"\t {f_new_name}")

    # Rename the file, being sure to use path/filename
    os.rename(f, f_new_name)
/Users/Tobias/workshop/buildbox/neurecycle/image_data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10821098423056008, 'fraction_numerical': 0.04948341489940185, 'mean_word_length': 3.7058823529411766, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '31200', 'license': 'MIT', 'n_tokens_mistral': 731, 'n_tokens_neox': 646, 'n_words': 198, 'path': 'notebooks/file-manipulation.ipynb', 'repo_name': 'tobias-fyi/neurecycle', 'size': '1846'}