文件操作

文件操作:

博客园链接.text

参数:

1.文件路径

2.编码方式:utf-8

3.动作mode-读,读写,写读...

绝对路径:

f1=open('D:\空间.txt',encoding='utf-8',mode='r')
content=f1.read()
print(content)

相对路径:
f1=open('lo',encoding='utf-8')
content=f1.read()
print(content)
f1.close()


f1,文件句柄,文件对象,变量,file,f_handle,file_handle,f_obj

open,打开的指令,它是windows指令,调用windows指令,

window 默认编码方式gbk,linux默认编码方式utf-8,mac utf-8

1,打开文件,产生文件句柄
2,操作文件句柄
3,关闭文件

r 模式:文字类的文件操作

 rb 模式:非文字类的文件操作

# readline 按行读取

# readlines 将每一行做为列表的元素,并返回列表

# for 循环  

f2=open('lo',encoding='utf-8')
print(f2.readline())
f2.close()
ded 哈哈
f2=open('lo',encoding='utf-8')
print(f2.readlines())
f2.close()

['ded 哈哈\n', 'dljgjg']

 

f2=open('lo',encoding='utf-8')
for i in f2:
print(i)
f2.close()

ded 哈哈

dljgjg

 

posted @ 2018-04-16 22:54  dongyanyan  阅读(125)  评论(0编辑  收藏  举报