码迷,mamicode.com
首页 > 编程语言 > 详细

python note 08 文件操作

时间:2019-03-17 15:20:13      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:write   txt   coding   int   class   pytho   内容   div   清除   

1、相对路径与绝对路径比较

#绝对路径

f = open(d:\pzw.txt,mode=r,encoding=UTF-8)
content = f.read()
print(content)
f.close()

#相对路径

f = open(pzw,mode=r,encoding=utf-8)
content = f.read()
print(content)
f.close()

 

2、

#对于r就是只读

f = open(pzw,mode=rb,)
content = f.read()
print(content)
f.close()

#对于w没有文件会创建文件,有文件会将源文件的内容全部清除再写

f = open(log,mode=w,encoding=utf-8)
f.write(123456)
f.close()

 

f = open(log,mode=wb)
f.write(123456.encode(utf-8))
f.close()

 

f = open(log,mode=w+,encoding=utf-8)
f.write(aaa)
print(f.read())
f.close()

 

#对于a为追加功能

f = open(log,mode=a,encoding=utf-8)
f.write(666)
f.close()

 

f = open(log,mode=ab)
f.write(666.encode(utf-8))
f.close()

 

f = open(log,mode=a+,encoding=utf-8)
f.write(666)
f.seek(0)
print(f.read())
f.close()

 

python note 08 文件操作

标签:write   txt   coding   int   class   pytho   内容   div   清除   

原文地址:https://www.cnblogs.com/alifetimelove/p/10546987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!