python文件學習

all=open("w.txt").read()
all_n=open("w.txt",'rb').read()
print all
print all_n
file_object=open('w.txt')
list_of_all_the_lines=file_object.readlines()
print list_of_all_the_lines

file_object=open('w.txt')
try:
    all_the_text=file_object.read()
finally:
    file_object.close()
    print all_the_text    

結果:

oh my god! what happen?so funny!
oh my god! what happen?so funny!
['oh my god! what happen?so funny!']
oh my god! what happen?so funny!

當文件中包含換行的時候
file_object=open('w.txt')
list_of_all_the_lines=file_object.readlines()
file_object=open('w.txt')
list_of_all_the_lines1=file_object.read().split('\n')
file_object=open('w.txt')
list_of_all_the_lines2=[L.rstrip('\n') for L in file_object]
print list_of_all_the_lines
print list_of_all_the_lines1
print list_of_all_the_lines2

['oh my god! \n', 'what happen?\xa1\xa2\n', 'so funny!']
['oh my god! ', 'what happen?\xa1\xa2', 'so funny!']
['oh my god! ', 'what happen?\xa1\xa2', 'so funny!']


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章