Search code examples
pythonemojislack

Python: Take a list of filenames and convert them to a specific format


I have a large list of filenames that I'd like to convert to a specific format.

e.g.

"D:\emote\aaatoot.gif"
"D:\emote\aatrek.001.gif"
"D:\emote\agesilaus.001.png"
"D:\emote\agreed.001.png"
"D:\emote\anarchists.001.gif"
etc...

into

emojis:
  - name: aaatoot
    src: D:\emote\aaatoot.gif
  - name: aatrek.001.gif
    src: D:\emote\aatrek.001.gif.gif
etc...

I'm trying to turn a folder of images into an emojipack which will be imported into slack, if that's relevant. Any help would be great.


Solution

  • you can read the file and format like this:

    import os
    with open("your_file") as f, open("new_file", "w") as f1:
        f1.write("Emojis:\n")
        for line in f:
            line = line.strip()
            name = os.path.basename(line)
            f1.write("\t- name: {}\n\t  src:{}\n".format(name, line))