如何使用 Python 在 Selenium 中編寫文字檔案?
我們可以透過先建立一個文字檔案,並在其中放置內容,然後用 Python 在 Selenium 中編寫文字檔案。
首先,我們需要以寫模式開啟檔案,並將文字檔案位置的路徑作為引數提及。有多種讀取方法可以執行這些操作。
write() – 它將字串一行寫入到文字檔案中。
writelines() – 它將多個字串寫入到文字檔案中。
示例
使用 write() 進行程式碼實現。
#open the file for write operation f = open('hello.txt' , 'w') #writes the new content f.write('Tutorialspoint') #close the file f.close() # again open the file for read f = open('hello.txt' , 'r') #reads the file content and prints in console print(f.read()) #close the file f.close()
使用 writelines() 進行程式碼實現。
#open the file for write operation f = open('hello.txt' , 'w') lines = ["Tutorialspoint", "Selenium"] #writes the new content f.writelines(lines) #close the file f.close() # again open the file for read f = open('hello.txt' , 'r') #reads the file content and prints in console print(f.read()) #close the file f.close()
廣告