my understanding is file.write()
takes string
as argument and write it as a whole into file, while file.writelines()
takes list of strings
as argument and write it to file. Then here is my test:
file_name = "employees"
content = "this is first employee\nthis is second employee\nthis is thirdemployee\n"
def write_lines(name: str, lines: list) -> None:
with open(name, "w") as file:
file.writelines(lines)
def read(name: str) -> None:
with open(name) as file:
print(file.read())
write_lines(file_name, content)
read(file_name)
surprisingly, it runs successfully and here is the result
this is first employee
this is second employee
this is thirdemployee
the result is actually the same as using file.write(). so, what's the difference between file.write()
and file.writelines()
? am I right on the previous understanding?