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个字符,但是这里不需要
结果图
python 读取全部文件名以及路径,添加固定字符,并且输出到txt_第1张图片

你可能感兴趣的:(UBuntu)