Python:获取文件名列表并将它们转换为特定格式

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

我有一大堆文件名要转换成特定格式。

例如

"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...

进入

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

我正在尝试将一个图像文件夹转换为一个 emojipack,如果相关的话,它将被导入到 slack 中。 任何帮助都会很棒。

你可以这样读取文件和格式:

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))