Back to Everyone Can Use English

get all md file name start wtih 3.2.

1000-hours/public/jupyter-notebooks/rename.ipynb

0.7.91.9 KB
Original Source
python
import os

# get all md file name start wtih 3.2.
md_files = [f for f in os.listdir('.') if f.startswith('3.2.') and f.endswith('.md')]
for f in md_files:
    # get string between 3.2. and -
    order = f.split('3.2.')[1].split('-')[0]
    new_name = f'3.2.{str(int(order)-1)}-{f.split("-")[1]}'
    # rename file
    os.rename(f, new_name)
python
text = """
                      {
                        text: "2.2.10. tʃ/dʒ",
                        link: "/sounds-of-american-english/2.2.10-tʃdʒ",
                      },
                      {
                        text: "2.2.11. tr/dr",
                        link: "/sounds-of-american-english/2.2.11-trdr",
                      },
                      {
                        text: "2.2.12. ts/dz",
                        link: "/sounds-of-american-english/2.2.12-tsdz",
                      },
                      {
                        text: "2.2.13. m, n, ŋ",
                        link: "/sounds-of-american-english/2.2.13-mnŋ",
                      },
                      {
                        text: "2.2.14. l, r",
                        link: "/sounds-of-american-english/2.2.14-lr",
                      },
                      {
                        text: "2.2.15. w, j",
                        link: "/sounds-of-american-english/2.2.15-wj",
                      },
                    ]
                  },
"""

lines = text.split('\n')
for line in lines:
    if "2.2." in line:
        order = line.split('2.2.')[1].split('.')[0]
        if len(order) <= 2:
            new_order = str(int(order)-2)
            line = line.replace(f'2.2.{order}', f'2.2.{new_order}')
        order = line.split('2.2.')[1].split('-')[0]
        if len(order) <= 2:
            new_order = str(int(order)-2)
            line = line.replace(f'2.2.{order}', f'2.2.{new_order}')
        line = line.replace('2.2.', '3.2.')
    print(line)