search

Home  >  Q&A  >  body text

python - 如何循环下输出

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()

.....

.........

如何循环下输出

PHPzPHPz2887 days ago411

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:35:28

    # 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()

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 17:35:28

    for name in hellos:
        with open(name+'.txt', 'r') as f:
             f.write(globals[name].text)

    reply
    0
  • Cancelreply