python 示例_带有示例的Python File read()方法
python 示例
文件read()方法 (File read() Method)
read() method is an inbuilt method in Python, it is used to read the content of the file, by using this method we can read the specified number of bytes from the file or content of the whole file.
read()方法是Python中的內置方法,用于讀取文件的內容,通過使用此方法,我們可以從文件或整個文件的內容中讀取指定數目的字節。
Syntax:
句法:
file_object.read(size)Parameter(s):
參數:
size – It is an optional parameter, it specifies the number of bytes to be read from the file. It's default value is -1 that returns the content of the whole file.
size –這是一個可選參數,它指定要從文件讀取的字節數。 它的默認值是-1 ,它返回整個文件的內容。
Return value:
返回值:
The return type of this method is <class 'str'>, it returns the string i.e. file's content (if the file is in text mode).
此方法的返回類型為<class'str'> ,它返回字符串,即文件的內容(如果文件處于文本模式)。
Example:
例:
# Python File read() Method with Example# creating a file myfile = open("hello.txt", "w") # wrting text to the file myfile.write("C++ is a popular programming language.") # closing the file myfile.close()# reading the file i.e. opening file in read mode myfile = open("hello.txt", "r") # reading & printing the whole file # Here, we are not specifying the size print("myfile.read()...") print(myfile.read())# reset the position myfile.seek(0)# reading 10 bytes and printing print("myfile.read(10)...") print(myfile.read(10))# reset the position myfile.seek(0)# reading whole file by passing -1 print("myfile.read(-1)...") print(myfile.read(-1))# closing the file myfile.close()Output
輸出量
myfile.read()... C++ is a popular programming language. myfile.read(10)... C++ is a p myfile.read(-1)... C++ is a popular programming language.翻譯自: https://www.includehelp.com/python/file-read-method-with-example.aspx
python 示例
總結
以上是生活随笔為你收集整理的python 示例_带有示例的Python File read()方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: echarts 柱状图不显示y坐标轴_P
- 下一篇: 织梦cms生成首页html的php文件,