在python中,對於文件的讀取一般有三種方式,下面分別對這三種方式進行簡單的描述並輔以案例代碼:
1、readline()是按行讀取文件,默認獲取定義行的內容,見實例代碼:
def fileWrites():
f = open(r'E:/text.txt', 'w+')
li = ["selenium webdriver\n",'自動化測試\n']
f.writelines(li)
f.close()
# 單行讀取文件
def readFile():
f = open(r'E:/text.txt', 'r')
print(f.readline())
f.close()
if __name__ == '__main__':
fileWrites()
readFile()