[英]How to read content of text file using function
我是Python的新手。 遵循以下示例: https : //medium.freecodecamp.org/send-emails-using-code-4fcea9df63f
我有一個使用模板字符串“${PERSON_NAME}”
的消息模板文件message.txt。 該字符串將替換為該人的實際姓名。
因此,我正在使用此功能,但是出現了一些問題,因為它給了我奇怪的輸出”
from string import Template
def read_template(filename):
with open(r'C:\Users\MyUserName\Documents\SQL_NeedToKnow\Python\message.txt', 'r', encoding='utf-8') as template_file:
template_file_content = template_file.read(100)
return Template(template_file_content)
print(read_template(r'C:\Users\MyUserName\Documents\SQL_NeedToKnow\Python\message.txt'))
我想念什么?
您可以使用readlines()
函數讀取文件。
def read_from_file():
with open('file.txt', 'r') as file:
content = file.readlines()
print content
這將打印文件的全部內容。
同樣, readline()
函數將從文件中讀取一行,而readlines()
將讀取文件的全部內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.