Python file type

Source: Internet
Author: User

Although the Python programming language is a relatively easy-to-use computer application language, in practice, it still requires beginners to master the basic knowledge to operate this language well. For example, today we bring you a basic knowledge point about the Python file type application method.

  • Introduction to how to build a Python Development Environment
  • Introduction to the correct calling method of the Python Interpreter
  • Introduction to the correct implementation of Python AOP
  • How to compile Python code for capturing Web Images
  • Explanation of the specific application method of the Python submission form

The Python file type is a special type in Python, which is used to operate external files in Python programs. In Python, everything is an object, and file is no exception. file has the methods and attributes of file. The following describes how to create a file object:

* File (name [, mode [, buffering])

The file () function is used to create a file object. It has a v alias called open (), which may be more vivid. They are built-in functions. Let's take a look at its parameters. Its parameters are all transmitted in the form of strings. Name is the name of the file.

Mode is the open mode. The optional value is r w a U, which indicates the default read mode.) Write mode supports various linefeeds. If you open a file in w or a mode, if the file does not exist, it is automatically created. In addition, when you open an existing file in w mode, the content of the original file will be cleared, because the operation mark of the file at the beginning is at the beginning of the file, at this time, the write operation will undoubtedly erase the original content.

For historical reasons, line breaks have different modes in different systems. For example, in unix, the line breaks are \ n, and in windows, the line breaks are \ r \ n ', opening a file in U mode supports all line breaks. That is to say, '\ r'' \ n' \ r \ n' indicates line breaks, there will be a tuple used to store the line breaks used in this file. However, although there are multiple line breaks, reading Python should be replaced by \ n. + B t can be added after the pattern character to indicate that the file can be read and written at the same time, and the file can be opened in binary and text modes by default.

If buffering is set to 0, no buffer is performed. If it is set to 1, "Row buffer" is performed. If it is a number greater than 1, the buffer size is measured in bytes.

Python file objects have their own attributes and methods. Let's take a look at the file attributes.

* Closed # MARK whether the file is closed and rewritten by close ()

* Encoding # file encoding

* Mode # Open mode

* Name # file name

* Newlines # The line feed mode used in the file is a tuple

* Softspace # boolean type, generally 0, is said to be used for print

Read/write method of Python file type:

* F. read ([size]) # size indicates the read length, in bytes.

* F. readline ([size])

# Read a row. If the size is defined, only one part of the row may be returned.

* F. readlines ([size])

# Use each row of the file as a member of a list and return this list. In fact, it is implemented by calling readline () cyclically. If the size parameter is provided, size indicates the total length of the read content, that is, it may be read only to a part of the file.

* F. write (str)

# Write str to a file. write () does not add a linefeed after str.

* F. writelines (seq)

# Write All seq content to the file. This function is only faithfully written without adding anything to the end of each line.

The above is our introduction to the Python file type.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.