python正式学习第二天

用python操作文件

步骤一;找到文件,打开文件

步骤二;修改文件 ,读取文件

步骤三;关闭文件,并保存

用代码演示如下;

1. f = open(file nama)
2. f.read(#读取的字节) #不写默认读所有 写方法f.write(data)
3.f.close #关闭并保存
读取实列;
f = open(file = 'C:\\Users\\Public\\heloo',mode="w",encoding='utf-8')
f.write('欢迎\n')
f.write('xiaoma\n')
f.close()
写入实例;
f = open(file = 'C:\\Users\\Public\\heloo',mode='r',encoding='utf-8')
print(f.read())
f.close()
#注意事项 windows下文件路径要以\\分隔,不能用空格
下面是练习用的· 可以不用看
'''用python操作文件
1.找到文件,打开
2,读,修改
3 保存 或则 关闭
代码演示
1. f = open(file nama)
2. f.read(#读取的字节) #不写默认读所有 写 f.write(data)
3.f.close #关闭并保存
4.只能打开模式,只能用一种模式操作文件.
r read 读取
w write 创建 有就直接覆盖
a append 从文件最后面追加

f = open(file = 'C:\\Users\\Public\\heloo',mode="w")
f.write('欢迎\n')
f.write('xiaoma\n')
f.close()
f = open(file = 'C:\\Users\\Public\\mm',mode = 'w')
f.write('xiaoma 12345678901\n')
print('----------------分隔符号-----------')
f.close()
f = open(file = 'C:\\Users\\Public\\mm',mode='r')
test = f.readline()#读一行
print(test)
print('--------')
f.close()
f = open(file = 'C:\\Users\\马东\\PycharmProjects\\hihi.py',mode = 'w')
f.write('heloo world')
f.close()
f = open(file = 'C:\\Users\\马东\\PycharmProjects\\hihi.py',mode="r")
print(f.read())
f.close()
f = open(file = 'C:\\Users\\马东\\PycharmProjects\\www',mode='w',encoding = 'utf-8')
f.write('小马你好呀!')
f.close()'''
f = open(file = 'www',mode='r',encoding='utf-8')
print(f.read())
f.close()



猜你喜欢

转载自www.cnblogs.com/xiaolang666/p/11852925.html