Home > Article > Backend Development > Does python’s input source include file input?
Python’s input sources include file reading and keyboard 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!