Python生成txt文件,並添加內容 - 邱乘屹的個人技術博客

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

在這裏插入圖片描述

成功!文件名我使用了字符串拼接,不要在意

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章