1 #!/usr/bin/env python
2 #-*-coding:utf-8-*-
3 __author__ = ''
4 import os
5
6 def BuildFile():
7 print(u"创建一个名字为test.txt的文件,并在其中写入hello python")
8 print(u"先保证test.txt这个文档不存在")
9 os.system('rm test.txt')
10 os.system('ls -l test.txt')
11 print(u"现在往里面写入内容")
12 fp = open('test.txt','w')
13 fp.write('hello world!')
14 fp.close()
15 print(u"写完不要忘记关闭")
16 print(u"接下来看看内容和文件是否存在")
17 os.system('ls -l test.txt')
18 os.system('cat text.txt')
19 print('/n')
20
21 print(u"如何避免open文件失败的问题呢?")
22 print(u"使用with as 就行了")
23 with open('test.txt','r') as fp:
24 st = fp.read()
25 print('test.txt的内容位:%s' %st)
26
27 if __name__ == '__main__':
28 BuildFile()