加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

17 Python 3

发布时间:2023-04-15 14:10:48 所属栏目:教程 来源:
导读:This chapter covers all the basic I/O functions available in Python 3. For more functions, please re

This chapter covers all the basic I/O functions available in Python 3. For more functions, pleas
This chapter covers all the basic I/O functions available in Python 3. For more functions, please re

This chapter covers all the basic I/O functions available in Python 3. For more functions, please refer to the standard Python documentation.

Printing to the Screen
The simplest way to produce output is using the print statement where you can pass zero or more expressions separated by commas. This function converts the expressions you pass into a string and writes the result to standard output as follows −

Live Demo
#!/usr/bin/python3
 
print ("Python is really a great language,", "isn't it?")
This produces the following result on your standard screen −

Python is really a great language, isn't it?
Reading Keyboard Input
Python 2 has two built-in functions to read data from standard input, which by default comes from the keyboard. These functions are input() and raw_input()

In Python 3, raw_input() function is deprecated. Moreover, input() functions read data from keyboard as string, irrespective of whether it is enclosed with quotes ('' or "" ) or not.

The input Function
The input([prompt]) function is equivalent to raw_input, except that it assumes that the input is a valid Python expression and returns the evaluated result to you.

#!/usr/bin/python3
 
>>> x = input("something:")
something:10
 
>>> x
'10'
 
>>> x = input("something:")
something:'10' #entered data treated as string with or without ''
 
>>> x
"'10'"
opening and Closing Files
Until Now, you have been reading and writing to the standard input and output. Now, we will see how to use actual data files.

Python provides basic functions and methods necessary to manipulate files by default. You can do most of the file manipulation using a file object.

The open Function
Before you can read or write a file, you have to open it using Python's built-in open() function. This function creates a file object, which would be utilized to call other support methods associated with it.

Syntax
file object = open(file_name [, access_mode][, buffering])
Here are parameter details −

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

access_mode − The access_mode determines the mode in which the file has to be opened, i.e., read, write, append, etc. A complete list of possible values is given below in the table. This is an optional parameter and the default file access mode is read (r).

buffering − If the buffering value is set to 0, no buffering takes place. If the buffering value is 1, line buffering is performed while accessing a file. If you specify the buffering value as an integer greater than 1, then buffering action is performed with the indicated buffer size. If negative, the buffer size is the system default(default behavior).

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章