1、创建文件,写入内容
w的意思是如果该路径没有该文件,可以自动创建。
file = open('/Users/caohui/Desktop/file.txt','w')
file.write('Hello World!')
2、函数方式:创建文件并写入
#coding:utf-8
def text_create(name,msg):
desktop_path = '/Users/caohui/Desktop/'
full_path = desktop_path+name+'.txt'
file = open(full_path,'w')
file.write(msg)
file.close()
print ('Done')
text_create('hello','hello world')