我知道这个问题的其他解决方案,例如 usingwith open as
,但首先我想了解为什么我的代码是或不是一个好的解决方案。
我正在尝试打开两个 CSV 文件,一个用于读取,一个用于写入。仅当成功打开两个文件时,脚本才应继续。我的代码似乎可以做到这一点,但我想知道几件事:
- 完成此任务的最 Pythonic 方式是什么,为什么?
- 从辅助函数中退出脚本是不好的做法吗?
原始代码:
input_file = 'in_file.csv'
output_file = 'out_file.csv'
def open_file(file, mode):
try:
fp = open(file, mode)
except IOError as e:
print "Error: cannot open {0}".format(file)
if e.errno == errno.EACCES:
print "\tPermission denied."
print "\tError message: {0}".format(e)
sys.exit()
# Not a permission error.
print "\tDoes file exist?"
print "\tError message: {0}".format(e)
sys.exit()
else:
return fp
def main():
# Open files in binary read/write mode for platform independence.
out_csv = open_file(output_file, 'wb')
in_csv = open_file(input_file, 'rb')
# Do stuff with the files
#
# with out_csv:
#
# writer = csv.writer(out_csv, delimiter='\t')
#
# with in_csv:
#
# reader = csv.reader(in_csv, delimiter='\t')
# for row in reader:
if __name__ == '__main__':
main()
编辑:使用 Python 2.7.2
编辑:草稿代码:
input_file = 'in_file.csv'
output_file = 'out_file.csv'
def main():
try:
with open(input_file, 'rb') as in_csv, open(output_file , 'wb') as out_csv:
writer = csv.writer(out_csv, delimiter='\t')
reader = csv.reader(in_csv, delimiter='\t')
for row in reader:
# continue processing
# many lines of code...
except IOError as e:
print "Error: cannot open {0}".format(file)
if e.errno == errno.EACCES:
print "\tPermission denied."
print "\tError message: {0}".format(e)
sys.exit()
# Not a permission error.
print "\tDoes file exist?"
print "\tError message: {0}".format(e)
sys.exit()
if __name__ == '__main__':
main()
我的草稿代码在 try 语句中感觉有点臃肿(想象一下另外 100 行代码)。有更好的方法吗?