设计自己的函数

设计这样一个函数,在桌面的文件夹上创建10个文本,以数字给它们命名

代码如下:

1
2
3
4
5
6
7
8
def text_creation():
path = '/Users/zhengzhenzhen/Desktop/'
for name in range(1,11):
with open(path + str(name) + '.txt','w') as text:
text.write(str(name))
text.close()
print 'Done'
text_creation()

另一种

1
2
3
4
5
6
7
8
def text_creat(name,msg):
desktop_path = '/Users/zhengzhenzhen/Desktop/'
full_path = desktop_path + name + '.txt'
file = open(full_path,'w')
file.write(msg)
file.close()
print 'Done'
text_creat('hello','hello world')