Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the operation method of IO in python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article analyzes "what is the operation method of IO in python". The content is detailed and easy to understand. Friends who are interested in "what is the operation method of IO in python" can follow the editor's train of thought to read it slowly and deeply. I hope it will be helpful to everyone after reading. Let's follow the editor to learn more about "what is the operation method of IO in python".

Python File I Dot O

Print to the screen:

The easiest way to output is to use a print statement, which you can pass zero or more expressions separated by commas.

Read keyboard input:

Python provides two built-in functions to read a line of text from standard input, and the default standard input is the keyboard. As follows:

Raw_input

Input

Raw_input function

The raw_input ([prompt]) function reads a line from standard input and returns a string (excluding the newline character at the end):

Str = raw_input ("Please enter:") print ("what you enter is:", str) > Please enter: (enter what you want to enter here) (what is printed here is your output above) the input function

The input ([prompt]) function is basically similar to the raw_input ([prompt]) function, but input can take an Python expression as input.

Open and close files

You must use Python's built-in open () function to open a file and create a file object before the related methods can call it for reading and writing.

Open function

You must first use Python's built-in open () function to open a file and create a file object before the related methods can call it for reading and writing.

File object = open (file_name [, access_mode] [, buffering])

The file_name:file_name variable is a string value that contains the name of the file you want to access.

Access_mode:access_mode determines the mode of opening files: read-only, write-only, append, etc. All available values are shown in the complete list below. This parameter is not mandatory, and the default file access mode is read-only ®.

Buffering: if the value of buffering is set to 0, there will be no hosting. If the value of buffering is 1, the line is deposited when the file is accessed. If you set the value of buffering to an integer greater than 1, this is the buffer size of the register area. If you take a negative value, the buffer size of the register area is the system default.

The following is a complete list of open files in different modes:

Properties of the file object

After a file is opened, you have a file object, and you can get all kinds of information about the file.

The following is a list of all the properties related to the file object:

Examples are as follows:

Fo = open ("xiaochan.txt", "w") print ("file name:", fo.name) print ("whether closed:", fo.closed) print ("access mode:", fo.mode) print ("whether to force a space at the end:", fo.softspace) output: file name: xiaochan.txt is closed: False access mode: whether to force a space at the end of w: 0close () method

The close () method of the File object flushes any unwritten information in the buffer and closes the file, after which no further writes can be made. When a reference to a file object is reassigned to another file, Python closes the previous file. It is a good habit to close the file with the close () method.

# Open a file fo = open ("xiaochan.txt", "w") print ("file name:", fo.name) # close the open file fo.close ()

The file object provides a series of methods to make it easier for us to access files, using the read () and write () methods to read and write files.

Write () method

The write () method writes any string to an open file. It is important to note that Python strings can be binary data, not just text. The write () method does not add a newline character ('\ n') at the end of the string. W means to open a file for writing only. The example is as follows:

# Open a file fo = open ("xo.txt", "w") fo.write ("www.soyboke.com\ nVery good blog!\ n") # close the open file fo.close () output: www.runoob.com

The read () method reads a string from an open file. It is important to note that Python strings can be binary data, not just text, as an example (using the xo.txt file above): the syntax is as follows:

FileObject.read ([count])

In this case, the parameter passed is the count of bytes to be read from the open file. This method reads from the beginning of the file, and if no count is passed in, it tries to read as much as possible, probably until the end of the file.

# Open a file fo = open ("xiaochan.txt", "r +") str = fo.read (10) print ("read string is:", str) # close the open file fo.close ()

Output:

Www.soybok

Python with open as function

You can repeatedly call write () to write to the file, but be sure to call f.close () to close the file. When we write a file, the operating system often does not write the data to disk immediately, but caches it in memory and writes it slowly when we are free. Only when the close () method is called does the operating system guarantee that all unwritten data is written to disk. The consequence of forgetting to call close () is that only part of the data may be written to disk and the rest is lost. So, use the with statement to get insurance:

* * with open as reads a file in the following format:

With open ('a.txtforth,' w') as f: what are the data types of f.write ('1234567890\ n') python? the data type of python: 1. Numeric types, including int (integer), long (long integer), and float (floating point). two。 String, which is of type str and type unicode, respectively. 3. Boolean, Python Boolean is also used for logical operations and has two values: True (true) and False (false). 4. List, the list is the most frequently used data type in Python, and any data type can be placed in the collection. 5. Tuples are identified by "()", and internal elements are separated by commas. 6. A dictionary is a collection of key-value pairs. 7. A collection is an unordered, non-repeating combination of data.

So much for sharing what is the operation method of IO in python. I hope the above content can improve everyone. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report