File operations in Python

Index

• What is the file?
• How to open the file?
• How to shut up the file using Python?
• How to write a picture with Python?
• How to read Python file?
• Python file

What is the file?

The file name is the disk space to store information related to it. It is used to store data permanently with unforeseen memory (for example, hard drive).
                                             
Because, the official memo (RAM) is a huge amount of data on the computer, the file is used for the future use of the data. If we want to read or write a file we have to work first. If we did, it should be closed, so the resources are linked to the release file.

Therefore, Python, a file operation is done in the following series.
1. Open the file
2. Read or write (to perform the operation)
3. Close the file

How can this be done?

Python has an open key () to open the file. This technique returns to the product file, also known as the band, as it is used to read or modify files in accordance with it.

>>> f = open("test.txt")    # open file in current directory
>>> f = open("C:/Python33/README.txt")  # specifying full path
 We can decide how to open the file. In the case, we would say if we want to read 'r', write 'w' or add 'a' to present it. We can also say that we want to open the file format or optional option.
The levels read the text. In this case, we get a letter when reading the file.
On the other hand, the optional method returns to this method when using non-text files, such as the EXE file.

Mode
Description
'r'
Open a file for reading. (default)
'w'
Open a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
'x'
Open a file for exclusive creation. If the file already exists, the operation fails.
'a'
Open for appending at the end of the file without truncating it. Creates a new file if it does not exist.
't'
Open in text mode. (default)
'b'
Open in binary mode.
'+'
Open a file for updating (reading and writing)

f = open("test.txt")      # equivalent to 'r' or 'rt'
f = open("test.txt",'w')  # write in text mode
f = open("img.bmp",'r+b') # read and write in binary mode
 Unlike in other languages, 'a' character does not show up to 97 as defined by ASCII (or similar symptoms).
On the other hand, depending on the voting cell. Window, 'CP1252' but 'UTF-8' for Linux.
Therefore, we will also depend on the size of the application or another number that I will practice in a variety of different ways.
Therefore, when working with text files, it is highly recommended to determine the type of voting.

How to shut up the file using Python?

Once we have completed the file operation, the file should be properly installed.

f = open("test.txt",encoding = 'utf-8')
# perform file operations
f.close()


The folder closure will release the file attributes in the file and use Python () near.
Python has a collection of rubbish to clean undamaged items, but we do not need to rely on the file closure.

try:
   f = open("test.txt",encoding = 'utf-8'
   # perform file operations
finally:

   f.close()

How to write a picture of Python?

To write the Python file, we must do the 'w' writing, adding the 'x' 'o'.
We must take care of the 'w' model, since he writes the file if it is already there. All the above data is deleted.

with open("test.txt",'w',encoding = 'utf-8') as f:
   f.write("my first file\n")
   f.write("This file\n\n")

   f.write("contains three lines\n")

Writing letters or series of sequences (binary files) is done in the writing process (). This method returns the number of characters in the file.

How to read the Python file?

To read the Python file, we must open the file format.
There are many methods available for this purpose. We can do (size) the reading process to read the data size. If the measurement is not specified, it reads and returns at the end of the file.

>>> f = open("test.txt",'r',encoding = 'utf-8')
>>> f.read(4)    # read the first 4 data
'This'
>>> f.read(4)    # read the next 4 data
' is '
>>> f.read()     # read in the rest till end of file
'my first file\nThis file\ncontains three lines\n'
>>> f.read()  # further reading returns empty sting
''

 Python File Methods:


There are many methods available and the product folder. Some were used in previous models.
Here is a complete list of texts with a brief explanation.

Method
Description
close()
Close an open file. It has no effect if the file is already closed.
detach()
Separate the underlying binary buffer from the TextIOBaseand return it.
fileno()
Return an integer number (file descriptor) of the file.
flush()
Flush the write buffer of the file stream.
isatty()
Return True if the file stream is interactive.
read(n)
Read atmost n characters form the file. Reads till end of file if it is negative or None.
readable()
Returns True if the file stream can be read from.
readline(n=-1)
Read and return one line from the file. Reads in at most nbytes if specified.



Comments

Popular posts from this blog

Python – Functions

What is Django in python?

Python Shallow Copy and Deep Copy