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