The user input data into a file, the file must be non-existent
(1) Start line
(2) Module document string
(3) Import module
(4) Global variable declaration
(5) class definition
(6) Function definition
(7) Program body
#!/usr/bin/env python
Import OS//importing OS module
Content = []//define a list
#输入一个文件名, re-enter the prompt if present
While True:
fname = Raw_input ("FileName:")
If not os.path.exists (fname):
Break
Print "%s is already exists, Try again"% fname
#将用户输入的内容保存到一个列表当中
While True:
data = Raw_input ("Enter line (. To quit) > ")
if data = = ".":
Break
Content.append (data)
#将列表中的内容写入到文件中
Fobj = open (fname, ' W ')
Fobj.writelines (["%s\n"% x for x in content])
Fobj.close ()
This article is from the "Court of the Odd Tree" blog, please be sure to keep this source http://zhangdl.blog.51cto.com/11050780/1828784
Writing: Python Good example of code style parsing