今天,小编教大家用python制作一个文件添加器。
目录
1,介绍制作步骤
2,完整代码
1,制作步骤
(1)写入文件的路径
desktop_path = "路径,例如:C:\\Users\\用户名\\(OneDrive)\\desktop\\"
(2)可以创建.txt或.doc文件
full_path = desktop_path + name + 'txt或doc(后缀名)'
(3)自行理解(考验时间到)
file = open(full_path, 'w')
(4)msg也就是下面的Hello world!
file.write(msg)
(5)最后写入(输出)
text_create('你好', '我们将在五分钟后自毁电脑并弹出c盘,请保存好文件,祝你生活愉快:)')
2,完整代码
# 创建一个txt文件,文件名为mytxtfile,并向文件写入msg
def text_create(name, msg):
desktop_path = "C:\\Users\\60110\\OneDrive\\桌面\\1" # 新创建的txt文件的存放路径
full_path = desktop_path + name + '.txt' # 也可以创建一个.doc的word文档
file = open(full_path, 'w')
file.write(msg) #msg也就是下面的Hello world!
# file.close()
text_create('你好', '我们将在五分钟后自毁电脑并弹出c盘,请保存好文件,祝你生活愉快:)')
# 调用函数创建一个名为mytxtfile的.txt文件,并向其写入Hello world!
3,结果
小编第一次做,有问题说一下