현재 "파이썬을 배우는 것이 힘든 방법"이며 16 장에 도달했습니다.
파일 쓰기 후에 파일 내용을 인쇄 할 수 없습니다. 아무것도 인쇄하지 않습니다.파일 쓰기 후 파일을 읽을 수 없습니다.
from sys import argv
script, filename = argv print "We are going to erase the contents of %s" % filename print "If you don\'t want that to happen press Ctrl-C"
print "If you want to continue press enter"
raw_input("?") print "Opening the file..." target = open(filename, "w")
print "Truncating the file..." target.truncate()
print "Now i am going to ask you for 3 lines"
line_1 = raw_input("Line 1: ")
line_2 = raw_input("Line 2: ")
line_3 = raw_input("Line 3: ")
final_write = line_1 + "\n" + line_2 + "\n" + line_3
print "Now I am going to write the lines to %s" % filename
target.write(final_write)
target.close
print "This is what %s look like now" %filename
txt = open(filename)
x = txt.read() # problem happens here
print x
print "Now closing file"
txt.close
는'target.close'이 ('target.close해야한다)를'대신 당신이해야 'with' 문을 사용하십시오. –
... 그리고'txt.close'는'txt.close()'여야합니다. –
그리고 write를위한 여는 이미 잘립니다. –