热线电话:13121318867

登录
2022-02-16 阅读量: 550
Python File(文件) 方法


1.1 非表格文件的读取

# 非表格文件(文本文件):TXT、CSV

# CSV和TXT文件没什么本质区别


1.1.1 使用open()方法

文件打开三个动作:打开open>>>读写read>>>关闭close

python文件读取语法:

open(file, mode='r')

更多请参考:https://www.runoob.com/python/file-methods.html


备注:open方法只能打开文本文件(只能用记事本打开的文件)

不能打开Excel文件


doc1=open('/Users/apple/python.study/python.forward/Python forwrd concept',encoding='utf-8',mode='r')
doc1.read()


1.1.2 增加上下文管理语句


python文件读取?上下文管理语句有什么作用?

如果没有close会导致文件没有被保存!!!


>>> 解决方案?with管理语句,异常处理

with open('') as X:

X.read()

with open('/Users/apple/python.study/python.forward/Python forwrd concept',encoding='utf-8') as doc2:
    text=doc2.read()
    print(text)







reference:

https://www.runoob.com/python/file-methods.html


44.0809
0
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子