在本教程中,我们将介绍Python输入/输出的基础知识,以及如何在Python程序中导入模块。前面我们学习了Python数据类型。你可以从中学到Python数据类型.
Python输入输出
I/O是指输入和输出。我们将学习python输入和输出的基本函数。
打印输出到屏幕
我们已经熟悉print()
function. It is used to print something to the screen.
print("Print something to the screen")
此外,我们可以在print函数中传递两个或多个不同的字符串,这些字符串由逗号(,)或加号(+)分隔。这样地;
print("This is first String " + "Followed by this string");
#Or can be written like this also
print("This is another String " , "Followed by another string");
接受用户输入
有时我们的程序可能需要读取用户提供的键盘输入。因此,我们将使用input()
function. The input()
function reads one line from standard input. Or in other words input function reads from keyboard input until a line feed is given ( i.e. Pressed Enter).
#takes input from keyboard and stores in a string
str = input("Enter your input: ");
#then we can print the string
print(str)
如果你运行这个程序,你会看到一个像下图这样的提示,等待你输入。在按Enter键之前,它将使用与您键入的相同数量的字符。
Python I/O和#8211;文件操作
有时我们需要读写文件。因此,有一些功能可用。
文件打开
打开我们使用的文件打开()功能。这是Python中的一个内置函数。
f = open("input_file.txt") # open "input_file.txt" file in current directory
f = open("C:/Python34/README.txt") # specifying full path of a file
也可以指定打开该文件的模式或用途。
f = open("input_file.txt","r") # open "input_file.txt" file to read purpose
f = open("input_file","w") # open "input_file.txt" file to write purpose
f = open("input_file","a") # open "input_file.txt" file to append purpose
文件关闭
当我们完成了一个文件,我们需要关闭它。为此,我们将使用关闭()功能。
f = open("input_file.txt") # open "input_file.txt" file
#do file operation.
f.close()
从文件读取
对于读取文件,有几个函数。在下面的程序中,我们将探讨这些函数。
- 您可以使用
read()
function.f = open("input.txt") # open "input.txt" file str=f.read(10) #read first 10 bytes from the file. print(str) #print first 10 bytes from the file. f.close()
- 你可以用逐行读取文件
readline()
function.f = open("input.txt") # open "input.txt" file str=f.readline() #read first line from the file. print(str) #print the first line. str=f.readline() #read second line from the file. print(str) #print the second line. f.close()
- 您还可以一次读取所有行并将这些行存储在字符串列表中。
f = open("input.txt") # open "input.txt" file str=f.readlines() #read all the lines from the file at once. and store as a list of string print(str) #print list of all the lines. f.close()
写入文件
因为在文件中写入内容非常简单,与文件读取类似。我们将使用write()
function.
f = open("output.txt","w") # open "output.txt" file
#write something in the file.
f.write("this is my first line
")
f.write("this is my second line
")
f.close()
Python导入
每当我们想在Python程序中使用一个包或模块时,首先我们需要使其可访问。因此,我们需要在程序中导入该包或模块。
我们有一个号码。我们要打印它的平方根。所以如果我们在下面写这个程序,它应该可以正常工作。
#get a variable having value 16
number=16
#square root this number.
number=sqrt(number)
print(number)
但是如果我们运行这个程序,它会显示这样的错误-
这是因为,sqrt()
function is under module name “math”. So if we want to use this function, we need to make this module accessible by importing the “math” module. So the correct code will be like this –
#first import math module
import math
#get a variable having value 16
number=16
#square root this number.
number=math.sqrt(number)
print(number)
因此,这就是Python输入输出和Python导入。有关这些主题的更多信息,您可以从下面的链接中查看正式的Python文档。 Python文档I/O Python导入