i had created a python program but it was not working. this program would take in the input for a file and then display the file contents . the only error i was getting is a syntax error well i could not find the error . Please help me .
the code was :-
nm = input(“enter file name “)
str = raw_input(“enter ur text here: \n”)
f = open(nm,”w”)
f.write(str)
f.close()
print “1.See the file\n”
print “2.Exit\n”
s = input(“enter ur choice “)
if s == 1 :
fi = open(nm,”r”)
cont = fi.read()
for i in cont:
print i
else :
print “thank you “
The problem is that you are reading the filename using
input()instead ofraw_input(). See this answer that explains:Also, since you are reading in the file contents by using
fi.read(), your for loopfor i in cont:will select each character of the file’s contents one at a time, instead of each line. Something to be aware of!