IO Error 22 python -
infile1 = open("d:/p/non_rte_header_path.txt","r") infile2 = open("d:/p/fnsinrte.txt","r") line in infile1: item in infile2: eachfile = open(line,"r")
for above code getting below error. infile1 contains paths of may files d:/folder/src/em.h here \n automatically @ end of path.i not sure why happens. please help.
ioerror: [errno 22] invalid mode ('r') or filename: 'd:/folder/src/em.h\n'
everyone has provided comments telling problem if beginner don't understand why it's happening, i'll explain that.
basicly, when opening file python, each new line (when press enter key) represented "\n".
as read file, reads line line, unless remove "\n", line variable read
thethingsonthatline\n
this can useful see if file contains multiple lines, you'll want rid of it. edchum , alvits has given way of doing !
your corrected code :
infile1 = open("d:/p/non_rte_header_path.txt","r") infile2 = open("d:/p/fnsinrte.txt","r") line in infile1: item in infile2: eachfile = open(line.rstrip('\n'), "r")
Comments
Post a Comment