目前还是个Python菜鸟,在这留下Python的足迹,等待未来的我回忆现在的我。
编辑工具:Pycharm
# 定义创建文本并写入信息函数
def text_create(name, msg):
file_path = '/Users/zengjiamin/Desktop/'
full_path = file_path + name + '.txt'
file = open(full_path, 'w') # 打开一个完整路径,w代表:写入模式,如果没有就在该路径创建一个该名称的文本,如果有就附加。
file.write(msg) # 内容写入打开的文本
file.close() # 关闭文件
print('Done')
# 定义过滤函数,返回过滤后的文本内容
def text_filter(word, filter_word='zjm', change_word='cocoa'): # 三个参数,两个默认参数
return word.replace(filter_word, change_word)
# text_filter('hello zjm') 执行结果:hello cocoa
# 整合上面两个函数
def text_create_filter(name, msg):
clean_text = text_filter(msg) # 调用text_filter()函数,得到过滤后的内容
text_create(name, clean_text) # 调用text_create() 在指定路径创建文本
text_create_filter('try', 'zjm loves cocoa so much!')
# 执行结果:在指定路径有try.txt文件被创建,文本内容:cocoa loves cocoa so much!
(不可能阻止事物发展过程来理解事物!与君共勉)