Home  >  Article  >  Backend Development  >  Does python’s input source include file input?

Does python’s input source include file input?

anonymity
anonymityOriginal
2019-06-15 13:49:4810700browse

Python’s input sources include file reading and keyboard input.

Does python’s input source include file input?

Read keyboard input

Python provides the input() built-in function to read from standard input A line of text, the default standard input is the keyboard.

input can receive a Python expression as input and return the result of the operation.

#!/usr/bin/python3
str = input("请输入:");
print ("你输入的内容是: ", str)

This will produce the following results corresponding to the input:

请输入:ABC
你输入的内容是:  ABC

FileRead and write

open () will return a file object. The basic syntax format is as follows:

open(filename, mode)

filename: A string value containing the name of the file you want to access.

mode: Determines the mode of opening the file: read-only, write, append, etc. See the complete list of all possible values ​​below. This parameter is optional and the default file access mode is read-only (r).

#!/usr/bin/python3
# 打开一个文件
f = open("/tmp/foo.txt", "r")
str = f.read()
print(str)
# 关闭打开的文件
f.close()

The above is the detailed content of Does python’s input source include file input?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn