如何用python在Selenium中编写文本文件?
通过首先创建一个txt文件并在其中包含内容,我们可以使用python在Selenium中编写文本文件。
首先,我们需要以写模式打开文件,并以文本文件的位置路径作为参数。有多种读取方法可以执行这些操作。
write()–将字符串写在一个文本文件中。
writelines()–它在一个文本文件中写入多个字符串。
示例
用.NET实现代码write()。
#open the file for write operation
f = open('hello.txt' , 'w')
#writes the new content
f.write('Nhooo')
#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 = ["Nhooo", "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()