0
在python 2.7中,我需要對文件列表執行相同的操作。python讀取/寫入文件列表
例, #每個文件是例如,文件X =打開( 「someString」, 「A」)
fileList1 = [file1, file2, file3,file4,file5]
fileList2 = [file11, file21, file31,file41,file51]
allFilelist = [fileList1, fileList2]
當我嘗試讀/寫上他們的一些字符串,我得到一個文件描述符 :
line = item.readline()
IOError: [Errno 9] Bad file descriptor
# each file in allFilList is a file list
allFilList = [ifcxRpsFileNameL, ircxRpsFileNameL, transXRpsFileNameL, ifcxFileNameL,
ircxFileNameL, transXFileNameL]
for eachFileList in allFilList :
for item in eachFileList :
#print item.read
line = item.readline()
#for line in :
print "the line read from ", item, " is " , line
ll= line.strip("\n").split()
if len(ll) == 0 :
print "the file " , item , " is empty \n"
exit
elif len(ll) != TOTAL_ITR :
print "the len of the file " , item , " is not " , TOTAL_ITR , "\n"
exit
else:
item.write("\n")
lt = [float(num) for num in ll]
item.write(min(lt))
item.write(" ")
item.write(sum(lt)/len(lt))
item.write(" ")
item.write(max(lt))
item.write(" ")
item.write("\n")
item.close()
break
在迴應此評論:
打印出來的項目,然後再嘗試從中讀取數據和發佈輸出
輸出爲:<open file 'ND_ifxc_2010_RPS.dat', mode 'a' at 0x2ba38d1e9558>
打印出來'item'嘗試從中讀取數據,並張貼在輸出之前,請 – inspectorG4dget 2012-07-23 22:53:50
項打印輸出:<打開文件「ND_ifxc_2010_RPS.dat」模式的「a」 0x2ba38d1e9558 >,謝謝! – user1002288 2012-07-23 22:54:57
該文件已打開進行追加,但您正在嘗試從中讀取? – kojiro 2012-07-23 22:57:51