python生成txt文件,並添加內容
代碼如下
#導入os模塊
import os
#創建一個txt文件
def text_create(name, msg):
#自動獲取桌面路徑
desktop_path = os.path.join(os.path.expanduser('~'),"Desktop/")
full_path = desktop_path + name + '.txt' # 也可以創建一個.doc的word文檔
file = open(full_path, 'w')
file.write(msg)
text_create('my_txt','hello world')
#創建一個名爲my_txt的txt文件,內容爲hello world
成功!文件名我使用了字符串拼接,不要在意