-1
這是從終端輸出(8637,2) 這是代碼,不知道爲什麼有些數據丟失?在random_list.txt中找不到img_list.txt的最後40個條目。我錯過了什麼?python讀取txt文件丟失數據
import os
import argparse
import math
import pandas as pd
import numpy as np
example_dirname = os.path.abspath(os.path.dirname(__file__))
caffe_dirname = os.path.abspath(os.path.join(example_dirname,'../..'))
training_dirname = os.path.join(caffe_dirname,'data/101')
img_dirname = os.path.join(caffe_dirname,'data/101/101_ObjectCategories')
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description = 'Arrage the 101 data set')
parser.add_argument(
'train_percent',type = float, default = 0,
help= "the percent of the training data")
args = parser.parse_args()
img_num = 0
class_lable = 0
img_class = open('image_class.txt','w')
img_list = open('img_list.txt','wb')
# img_test = open('test.txt','w')
dirs = os.listdir(img_dirname)
for folder in dirs:
name = os.path.join(img_dirname, folder)
new_name = os.path.join(img_dirname, folder.lower())
if new_name != name:
os.rename(name,new_name)
dirs.sort()
for folder in dirs:
img_class.write(folder + ' ' + str(class_lable) + os.linesep)
temp = os.path.join(img_dirname,folder)
for file in sorted(os.listdir(temp)):
# print file
img_list.write(os.path.join(temp,file)+' '+str(class_lable)+ os.linesep)
img_num = img_num + 1
class_lable = class_lable + 1
print img_num
img_train = math.floor(args.train_percent * img_num)
df = pd.read_csv('img_list.txt',sep = " ", header = None)
print df.shape
# df.columns = ["img_path", "img_class"]
#df = df.iloc[np.random.permutation(img_num)]
df.to_csv("randomlist.txt", header=None, index=None, sep=' ', mode='w')
# df1 = df.iloc[:img_train]
# df2 = df.iloc[img_train:]
#print df1
img_class.close()
img_list.close()
使用'img_list.close()'在開始從''img_list.txt''讀取之前。當調用'close()'時,系統可能會將數據保存在緩衝區中並保存在文件中,而不是在調用write()時保存。 – furas
謝謝!有用。你的意思是如果我不關閉它,img_list的一些數據仍然在緩衝區中,所以當我使用read_csv時,我無法得到它? – user45690
是的,有些數據仍然在緩衝區中,並且close()將它們發送到文件。 – furas