"Python" instance-creates a file and enters characters via the keyboard

Source: Internet
Author: User
Import oslnend=os.linesep # #windows行结束符号是 "\ r \ n" filename=raw_input ("Please input FileName:") while true:# #检查该文件是否存在, When execution to break jumps out while loop    if Os.path.exists (FileName):        print "%s already exits"%filename        filename=raw_input ( ' Please input filename: '    else: Break   all=[]# #创建空的列表用于存储输入内容print ' please input words (OK to stop inputing) \ true:# #当执行到break时跳出while循环    words=raw_input (">>")    if words== "OK": Break    Else:        All.append (words) # #循环的往列表添加内容FileHandle =open (FileName, "W") Filehandle.writelines (["%s%s"% (x,lnend) for x in ALL] # #函数writelines (list) writes a list to a file, but does not add a line break after each element of the list, so you need to lnend the line break. The same write (str) writes STR to the file, and write () does not add a newline character after Str filehandle.close () print "Done"

  

"Python" instance-creates a file and enters characters via the keyboard

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.