Search code examples
pythonyamlmarkdownpython-relistdir

Concatenate 2 files with different files extension


apple.yml
apple.md
orange.yml
orange.md

I have files as .md and .yml I would like to concat them into one .md file if the filename match

What would be the next step here?

contents = {}
file_extension1 = "yml"
file_extension2 = "md"

# Get all files and directories that are in current working directory
for file_name in os.listdir('folder/'):
#     print(file_name)

    # Use '.' so it doesn't match directories
    if file_name.endswith(('.' + file_extension1 , '.'+ file_extension2)):
        print(file_name)

Solution

  • Here is how you can use the glob module:

    from glob import glob
    
    for f1 in glob("*.yml"): # For every yml file in the current folder
        for f2 in glob("*.md"): # For every md file in the current folder
            if f1.rsplit('.')[0] == f2.rsplit('.')[0]: # If the names match
                with open(f1, 'r') as r1, open(f2, 'r') as r2: # Open each file in read mode
                    with open(f"{new}_f2", 'w') as w: # Create a new md file
                        w.write(r1.read()) # Write the contents of the yml file into it
                        w.write('\n') # Add a newline
                        w.write(r2.read()) # Write the contents of the md file into it
    



    That code is for when there are inconsistent yml and md files. If all the yml files have a corresponding md file, and they are in a folder with other interfering yml and md files, you can:

    from glob import glob
    
    m = sorted(glob("*.md"))
    y = sorted(glob("*.yml"))
    
    for f1, f2 in zip(m, y):
        with open(f1, 'r') as r1, open(f2, 'r') as r2:
            with open(f"new_{f1}", 'w') as w:
                w.write(f1.read())
                w.write('\n')
                w.write(f2.read())
    


    UPDATE:

    To address the comment below:

    from os import listdir
    
    files = listdir()
    for f in files: # For every yml file in the current folder
        if f.endswith('.yml') and f.rsplit('.')[1]+'.md' in files:
            with open(f1, 'r') as r1, open(f.rsplit('.')[1]+'.md', 'r') as r2: # Open each file in read mode
                with open(f"{new}_f2", 'w') as w: # Create a new md file
                    w.write(r1.read()) # Write the contents of the yml file into it
                    w.write('\n') # Add a newline
                    w.write(r2.read()) # Write the contents of the md file into it