程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python reads the folder image name and saves it as a TXT file

編輯:Python

utilize python, Get the in the directory file with ‘.png’ Image with suffix , Save as list, And then write txt In file . Other types of files are similar , Modify the corresponding part .

# encoding: utf-8
import os
def to_text(src,dst1):
txt=[]
filenames= os.listdir(src)
# filenames.sort(key= lambda x:(int((x.split('_')[1]).split('.')[0]))) # train_2045.png
filenames.sort(key =lambda x: int(x.split('.')[0])) # 2045.png
#print(filenames)
for item in filenames:
if item.endswith('.png'):
txt.append(item)
fo=open(dst1,'w')
for item in txt:
fo.write(str(item)+'\n')
if __name__ == "__main__":
src=r'F:\all_date\WHU\A'
dst1=r'F:\all_date\WHU\file.txt'
to_text(src,dst1)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved