首页 > 开发 > Python > 正文

如何循环下输出

2017-09-06 19:28:24  来源:网友分享
fileHandle = open ( 'hello1.txt', 'w' )fileHandle.write(hello1.text)fileHandle.close()fileHandle = open ( 'hello2.txt', 'w' )fileHandle.write(hello2.text)fileHandle.close()fileHandle = open ( 'hello3.txt', 'w' )fileHandle.write(hello3.text)fileHandle.close()..............

如何循环下输出

解决方案

# coding:utf-8"""test.py循环创建列表中的文件,并把文件名写在文件内容里"""file_list=['a','b','c']for file_name in file_list:    with open(file_name+'.txt', 'w') as f:         f.write(file_name)         f.close()                           

file_list=['a','b','c']list_len=len(file_list)for i in xrange(0,list_len):    with open(file_list[i]+'.txt', 'w') as f:         f.write(file_list[i])         f.close()