python 示例_带有示例的Python File write()方法
生活随笔
收集整理的這篇文章主要介紹了
python 示例_带有示例的Python File write()方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
python 示例
文件write()方法 (File write() Method)
write() method is an inbuilt method in Python, it is used to write the content in the file.
write()方法是Python中的內(nèi)置方法,用于將內(nèi)容寫入文件中。
Syntax:
句法:
file_object.write(text/bytes)Parameter(s):
參數(shù):
text/bytes – Specifies the text to be written in the file.
文本/字節(jié) –指定要寫入文件的文本。
Return value:
返回值:
The return type of this method is <class 'int'>, it returns the total number of the written in the file.
此方法的返回類型為<class'int'> ,它返回文件中寫入的總數(shù)。
Example:
例:
# Python File write() Method with Example# creating a file myfile = open("hello.txt", "w")# writing to the file res = myfile.write("Hello friends, how are you?") print(res, "bytes written to the file.") # closing the file myfile.close()# reading content from the file myfile = open("hello.txt", "r") print("file content is...") print(myfile.read()) myfile.close();# writing more content to the file # opening file in append mode myfile = open("hello.txt", "a")# writing to the file res = myfile.write("Hey, I am good!") print(res, "bytes written to the file.") # closing the file myfile.close()# reading content from the file again myfile = open("hello.txt", "r") print("file content is...") print(myfile.read()) myfile.close()Output
輸出量
27 bytes written to the file. file content is... Hello friends, how are you? 15 bytes written to the file.file content is... Hello friends, how are you?Hey, I am good!翻譯自: https://www.includehelp.com/python/file-write-method-with-example.aspx
python 示例
總結(jié)
以上是生活随笔為你收集整理的python 示例_带有示例的Python File write()方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 光与夜之恋信使巡游地点怎么选择
- 下一篇: 恒温箱多少钱啊?