I wrote a script to read text file in python.
Here is the code.
parser = argparse.ArgumentParser(description='script')
parser.add_argument('-in', required=True, help='input file',
type=argparse.FileType('r'))
parser.add_argument('-out', required=True, help='outputfile',
type=argparse.FileType('w'))
args = parser.parse_args()
try:
reader = csv.reader(args.in)
for row in reader:
print "good"
except csv.Error as e:
sys.exit('file %s, line %d: %s' % (args.in, reader.line_num, e))
for ln in args.in:
a, b = ln.rstrip().split(':')
I would like to check if the file exists and is not empty file but this code gives me an error.
I would also like to check if program can write to output file.
Command:
python script.py -in file1.txt -out file2.txt
ERROR:
good
Traceback (most recent call last):
File "scritp.py", line 80, in <module>
first_cluster = clusters[0]
IndexError: list index out of range
in
is not a valid identifier (inargs.in
) – Jockofirst_cluster = clusters[0]
appear in your code? – Bowing