Python tutorial- write data to file

Write data to file

To write text to a file, we use write() method. The close() method will force the system to write data to the file. If you don’t write the close() method, the data are not written to the file immediately.

>>> open('Python_source\\test.txt','w')
>>> f=open('Python_source\\test.txt','w')
>>> f.write('Hello World')

To write data many lines of data to the file, you need to use writelines() method.
>>> f=open('Python_source\\test.txt','w')
>>> st='''While C, and C++ programs run fast, Python programs run slower.
But Python is easy to learn and save development time.'''
>>> f.writelines(st)
>>> f.close() >>>
python write to file


This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.