두 함수에 대해 print len () 값이 다른 이유는 무엇입니까? 동일하지 않습니까?
이 스크립트가 여는 파일은 세 줄의 텍스트가있는 텍스트 파일입니다. 나는 그것을 test.txt라고 명명했고 그 안에는
Jack and Jill
gave up
they went home with no water
암호:
def function2nd (filename):
target = open(theFile, 'r')
inData = target.read()
print inData
print len(inData)
target.close()
theFile = raw_input("What is the file name?\n>>")
function2nd(theFile)
def function3rd (filename):
target = open(theFile, 'r')
target.read()
print target.read()
print len(target.read())
target.close()
function3rd(theFile)
파일은 카세트에서 긴 테이프처럼 작동합니다. 파일을 읽을 수는 있지만 완료 할 때까지 테이프를 끝까지 통과 한 것입니다. 다시 읽으면 데이터가 다시 제공되지 않습니다 .
따라서 두 번째 함수는 이미 끝까지 감긴 파일에서 데이터를 읽으려고했습니다.
파일을 다시 열거 나를 사용하여 '테이프'를 되 감아 target.seek(0)
처음으로 되돌릴 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다