python 讀取全部文件名以及路徑,添加固定字符,並且輸出到txt

python 讀取全部文件名以及路徑,添加固定字符,並且輸出到txt

import os
txtName = "negatives.txt"
f=file(txtName, "a+")
dire="/home/a123/Downloads/OpenCV-Haar-AdaBoost-master/negative_images"
#anchor="/home/a123/Downloads/OpenCV-Haar-AdaBoost-master/positive_images" 
for root ,dirs, files in os.walk(dire):
    for file_single in files:
        test = root+'/'+file_single
        refile = file_single[0:8]
        #anchort = anchor + refile +'.png'
        result = test +'\n'
        #result = test + " " +anchort +'\n'
        f.write( result)
f.close()

file_single 是每個單個文件名
root是全部文件名
refile = file_single[0:8] 可以選擇表示選擇前8個字符,但是這裏不需要
結果圖
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章