[英]Read a file via argparse and print the file contents line by line
I want to 我想要
Code: 码:
import argparse
import csv
import os
parser = argparse.ArgumentParser(description = "Parse the json file")
parser.add_argument("-f", help="file to upload")
args = parser.parse_args()
fileName = args.f
with open(filename, 'rb') as f:
try:
for lines in f:
print(lines)
except:
print("The file " + fileName + " could not be opened.")
I cannot get the file lines to print out and the exception to run if the file is incorrect. 如果文件不正确,我将无法打印出文件行并无法运行异常。
Why are you opening in binary mode? 为什么以二进制模式打开? Also, your try/catch is in the wrong place
另外,您的尝试/接球位置错误
try:
with open(filename) as f:
for line in f:
print(line)
except:
print('The file ' + filename + ' could not be opened.')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.