Differences Between Write & Writeline in Python

Save

Python is a powerful, full-featured, free, open-source programming language used in education, science, government and industry. The Python language has a large standard library of functions that help programmers create Web applications, operating system utilities, games, scientific applications Web site frameworks and more. Python includes a comprehensive array of file handling functions to read, write, append, query, transfer and edit files. Dutch programmer, Guido van Rossum, known today as Python's "benevolent dictator," developed the language in the 1980s.

File Handling Methods For Objects

  • Python is an object-oriented programming language. Standard Python provides many methods or functions programmers can apply to objects. When the "dir()" function is applied to a file handle object, a list of the functions belonging to the file handle is displayed. For example, the Python command,

    nameOfFile= open("foo.txt", "rw+")

    opens up a file called "foo.txt" in a read/write mode and stores file information in the file object labeled "nameOfFile." The Python command dir(nameOfFile) prints out a list that includes all the functions belonging to the file handle object. Included in this list are the functions close, fileno, flush, isatty, newlines, next, read, readline, readlines, seek, tell, truncate, write and writelines.

The Python File Object Function Write()

  • Use the write() function to write a fixed sequence of characters -- called a string -- to a file. You cannot use write() to write arrays or Python lists to a file. If you try to use write() to save a list of strings, the Python interpreter will give the error, "argument 1 must be string or read-only character buffer, not list." Programmers usually ensure the data is actually saved on the disk by using the file object methods flush() or close().

The Python File Object Function Writelines()

  • The writelines() function also writes a string to a file. Unlike write(), however, writelines can write a list of strings without error. For instance, the command nameOfFile.writelines(["allen","hello world"]) writes two strings "allen" and "hello world" to the file foo.txt. Writelines() does not separate the strings, so the output will be "allenhello world."

The Origin of Pythons Writelines() Function

  • Python was first implemented in 1989. Prior to 1993, Python had the write() function but not writelines(). An early reference for the need for writelines() appeared in an October 22, 1993 Usenet newsgroup posting by Xerox Parc programmer Bill Janssen. He was looking for an easy way to add the contents of one file to another file. Three days later, the creator of Python, Guido Van Rossum wrote, "I just encountered the same need. Consider it added to the next release."

References

  • Photo Credit BananaStock/BananaStock/Getty Images
Promoted By Zergnet

Comments

Resources

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!