Home > Backend Development > Python Tutorial > Does python's input source include file input?

Does python's input source include file input?

anonymity
Release: 2019-06-15 13:49:48
Original
10767 people have browsed it

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)
Copy after login

This will produce the following results corresponding to the input:

请输入:ABC
你输入的内容是:  ABC
Copy after login

FileRead and write

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

open(filename, mode)
Copy after login

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()
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template