こんな感じかな
# import modules
import os.path
import csv
# functions
def csv2txt(inFilePath, outFilePath):
"""csv2txt convert csv file to text file.
Convert comma to tab."""
# file exists check
if not os.path.isfile(inFilePath):
print inFilePath + " is not exist."
return
# file extension check
root, ext = os.path.splitext(inFilePath)
if ext != ".csv":
print filepath + " is not .csv."
return
root, ext = os.path.splitext(outFilePath)
if ext != ".txt":
print filepath + " is not .txt."
return
# create output file
f = open(outFilePath,'w')
# read csv
reader = csv.reader(file(inFilePath, 'r'))
for row in reader:
newline = "\t".join(row)
f.write(newline + '\n')
f.close()
readerにclose()がなかった。自動的に開放されるみたい。
0 件のコメント:
コメントを投稿