import os txtName = "test.txt" f=file(txtName, "a+") dire="/media/tina/ET/caffe/SegNet_ip/CamVid/test/" anchor="/media/tina/ET/caffe/SegNet_ip/CamVid/testannot/" for root ,dirs, files in os.walk(dire): for file_single in files: test = dire+file_single refile = file_single[0:8] anchort = anchor + refile +'.png' result = test + " " +anchort +'\n' f.write( result) f.close()
沒寫絕對路徑,就會被認為是在當前文件夾,這個代碼主要是研究生階段各種實驗用,我從兩個功能代碼拼接的。當然可能有更簡潔美觀的,歡迎留言交流。root里面存了絕對路徑,不過由於我之后還要改,空格后面同一行的絕對路徑,以及文件格式,所以直接寫出來了。
這就是把待標注的圖像以及標簽寫到同一個txt。包括了他們的絕對路徑。用的挺多的。
這個代碼會輸出這樣的txt: