Python file write flush

How to write files in python

After study how to open and close file in Python , then open a file then we will write some random text into it by using the write() method . In order to write into a file in Python, we need to open it in write «w» for only writing (an existing file with the same name will be erased), append «a» or exclusive creation «x» mode. Since we didn’t specify a mode , the mode (default) was set to r.

my_file = open(«my_file.txt», «w») # Open a file my_file.write(«This is my first line») # write a line to the file my_file.write(«This is my second line») # write one more line to the file my_file.close() # close the file

After executing the above program, we can see the file is created in the disk. When opening the file, we can see the file content like:

It is because write() method does not add a newline character (‘\n’) to the end of the string. So, you need to explicitly add ‘\n’ to write write() method.

my_file = open(«my_file.txt», «w») # Open a file my_file.write(«This is my first line\n») # write a line to the file my_file.write(«This is my second line\n») # write one more line to the file my_file.close()

Now the contents of file look like:

By using «with» statement is the safest way to handle a file operation in Python because «with» statement ensures that the file is closed when the block inside with is exited.

Читайте также:  border-color

with open(«my_file.txt», «w») as my_file: my_file.write(«This is my first line\n») # write a line to the file my_file.write(«This is my second line\n») # write one more line to the file

Append data to text file in python

You can also append a new text to the already existing file or the new file. You need to open the file in append mode, by setting «a» or «ab» of «a+» as the mode. When you open with «a» mode, the write position will always be at the end of the file (an append). You can open with «a+» to allow reading, seek backwards and read (but all writes will still be at the end of the file).

with open(«my_file.txt», «a») as my_file: my_file.write(«This is my third line\n») # write a line to the file my_file.write(«This is my fourth line\n») # write one more line to the file

After executing the program and open the file then you can see the file content like:

Python flush()

The method flush() flushes the internal buffer, like stdio’s fflush. Python uses the operating system’s default buffering unless you configure it do otherwise. Python automatically flushes the files when closing them. But you can also force flush the buffer to a file programmatically with the flush() method .

with open(«my_file.txt», «w+») as my_file: my_file.write(«This is my first line \n») # do some work str = «This is my second line» my_file.write(str) my_file.write(‘\n’) my_file.flush() # some other work my_file.write(«This is my third line \n») my_file.flush()

Writing Unicode text to a text file in Python

The Unicode standard describes how characters are represented by code points . A code point is an integer value, usually denoted in base 16 . Python represents Unicode strings as either 16- or 32-bit integers, depending on how the Python interpreter was compiled. Best practice, in general, use UTF-8 for writing to files.

The utf-8 is the most modern and universally usable encoding and it works in all web browsers, most text-editors and most terminals/shells.

import io str = u’\u5E73\u621015′ with io.open(«my_file.txt», «w+», encoding=»utf-8″) as my_file: my_file.write(str)

As an alternative, you can use the codecs module . The low-level routines for registering and accessing the available encodings are found in the codecs module. A code point is an integer value, usually denoted in base 16. The most commonly used part of the codecs module is the codecs.open() function.

import codecs str = u’\u5E73\u621015′ with codecs.open(«my_file.txt», «w+», encoding=»utf-8″) as my_file: my_file.write(str)

Источник

Python File Handling

File handling is one of the important feature in programming languages. Python provides a way to handle(read, write & modify) both text files as well as binary files. This tutorial will cover peek(), write(), writelines(), flush() and close() functions in detail.

  • Open
  • Read
  • ReadLine
  • ReadLines
  • Seek
  • Tell
  • Peek
  • Write
  • WriteLines
  • Flush
  • Close
 fileVar = open("/complete_path_to_file/pythonTest.txt", "rb") print(fileVar.peek().decode('utf-8')) print(fileVar.tell()) Output: Line 1: This is Python File Handling Line 2: This is Python File Handling Testing File 0 #Even after peeking the data, position of cursor is Zero 
 fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "w") fileVar.write('This is Python File Writing') fileVar.close() Output: 27 *******pythonTestWrite.txt content******** This is Python File Writing ****************************************** 
 fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "w") fileVar.write('This is Python File Writing') fileVar.write('\nThis is Python\'s second File Writing line') fileVar.close() Output: 27 41 *******pythonTestWrite.txt content******** This is Python File Writing This is Python's second File Writing line ****************************************** 
 fileVar = open("/complete_path_to_file/pythonTestWritelines.txt", "w") file_str='''This is First Line This is Second Line This is third Line''' fileVar.writelines(file_str) fileVar.close() Output: 57 *******pythonTestWritelines.txt content******** This is First Line This is Second Line This is third Line *********************************************** 
 file_list =['This is First Line','This is Second Line', 'This is third Line' , 'This is fourth Line'] fileVar = open("/complete_path_to_file/pythonTestWritelines.txt", "w") fileVar.writelines(i+'\n' for i in file_list) fileVar.close() Output: *******pythonTestWritelines.txt content******** This is First Line This is Second Line This is third Line This is fourth Line *********************************************** 
 fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "w") fileVar.write('This is Python File Writing example for flush()') fileVar.flush() 
 fileVar = open("/complete_path_to_file/pythonTestWrite.txt", "w") fileVar.write('This is Python File Writing') fileVar.close() 

Источник

Оцените статью