如何使用 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()
广告
数据结构
计算机网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言程序设计
C++
C#
MongoDB
MySQL
JavaScript
PHP