我用自動熱鍵創建了這個,但我無法組織它們
^p::
Loop, filename*.png
{
SplitPath, A_LoopFileName,,,, name_no_ext
FileAppend, %name_no_ext%`n, filename%name_no_ext%.txt
}
所以我決定用 python 撰寫它,但我不能用 os lib 為每個檔案名制作文本檔案,我不能從文本檔案中的檔案名中洗掉點或自定義布局
這是我的代碼:
import os
import io
dir_path = 'user/to/my/path'
#first add incremental number to the file name
i = 1
for file in os.listdir(dir_path):
if file.endswith(".png"):
os.rename(file,'#' '{0:02d}'.format(i) file)
i =1
#now add text file for each then write there names of the files
a = io.open("output.txt", "w", encoding="utf-8")
for path, subdirs, files in os.walk(dir_path):
for filename in files:
f = os.path.join(filename)
f = os.path.splitext(filename)[0]
f = f.replace(' ', '\n')
a.write(str(f) "\n")
檔案名是這樣的:
Angel .Ghoul .Angry .black & waite .Cannon Pink ..png
并輸出此腳本而不重命名增量編號:
Angel
.Ghoul
.Angry
.black & waite
.Cannon Pink
我需要像這個第一個文本檔案:
#1
Angel
Ghoul
Angry
black & waite
Cannon Pink
第二個文本檔案:
#2
Angel
Ghoul
Angry
black & waite
Chelsea Cucumber
n 文本檔案:
#n
based on file name
based on file name
based on file name
based on file name
based on file name
檔案名增量編號的錯誤:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'Angel .Ghoul .Angry .black & waite .Cannon Pink ..png' -> '#01Angel .Ghoul .Angry .black & waite .Cannon Pink ..png'
uj5u.com熱心網友回復:
IIUC,嘗試:
import os
import re
dir_path = "path/to/your/folder"
files = [f for f in os.listdir(dir_path) if f.endswith(".png")]
for i, file in enumerate(files):
with open(f"#{i 1} {file.replace('.png', '.txt')}", "w") as outfile:
outfile.write(f"#{i 1}\n")
outfile.write("\n".join(re.split("\s\s ", file[:-4].replace(".","").strip())))
os.rename(file, f"#{i 1} {file}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/364706.html
上一篇:兩鍵字典合并為一鍵串列字典
下一篇:根據熊貓字典中另一列的值添加新列
