感覺是for下面的縮排問題,不過截圖看不出是space/ tab
可以發現for裡面的writer是沒有讀到東西的
可以參考
https://stackoverflow.com/questions/18952716/valueerror-i-o-operation-on-closed-file
先在for裡面測試file是不是已經被close了
更新:
Python 3.1後可以這樣玩
https://stackoverflow.com/questions/1990373/nesting-with-statements-in-python
# 同時讀檔與寫檔
with open('a.csv') as f, open('b.csv', 'w', newline='') as f2:
rows = csv.reader(f)
writer = csv.writer(f2)
writer.writerow(['test', 'test777'])
for row in rows:
writer.writerow(row)