python3.5.3rc1学习三:文件操作

##全局变量与局部变量
x = 6

def printFuc():
y = 8
z =9
print(y + z)
print(x)

printFuc()
#print(y)
#常见错误
##name = "Anthony"
##print(Name)

#文件写入

text = "Sample Text to Save New Line"

"""
调用buid-in函数:open打开或者创建文件,
如果exampleFile.txt不存在,就自动创建
w在这里表示可以写的模式,如果是读那就'r'
"""
saveFile = open("exampleFile.txt",'w')
saveFile.write(text)
saveFile.close()#操作后关闭

##如果你的demo.py文件在桌面,那么exampleFile.txt也会在桌面创建
##如果你要指定到特定路径你可以这样写
##saveFile = open('C:UsersAnthonyDesktopexampleFile.txt', 'w')

appendText = ' Append new line for testing.'

'''''
下面的'a',就是append的意思,后面讲列表会有append方法介绍
'''
saveFile = open("exampleFile.txt",'a')
saveFile.write(appendText)
saveFile.close()

readMe = open("exampleFile.txt",'r').read()
print(readMe)

原文地址:https://www.cnblogs.com/51testing/p/7895031.html