用python读取文件

文件读取

文件的声明和读取
最快的方法是txt和csv。 csv也是一个excel文件

路径书写的三种方式

# 关于路径的书写方式
path1 = "D:\\桌面文件\\Python\\test.txt"
path2 = "D:/桌面文件/Python/test.txt"
path3 = r"D:\桌面文件\Python\test.txt"

print(path1,path2,path3)

读取文件的方式

f = open(path1,'r',encoding='utf8')
print(type(f))
print(f)
print(f.read)
print('读取完毕')
open 的配置

open (”路径“,”模式“,encoding = ”编码“)
模式:
r:读取文件,默认;
w:写入
rw:读取+写入
a:追加

简单的读取犯法: .read() 读取后,光标会在末尾

read完成后,光标会在最后一个

需要用,seek()方法来将光标回到第一个

文件的读取与写入

文件读取

用绝对路径读取

f1 = open('D:/桌面文件/Python/新建文本文档.txt','r')
print(f1.read())

用相对路径读取

os.chdir('D:/桌面文件/Python')     #修改当前的操作位置

f2 = open('新建文本文档.txt','r')
print(f2.read())

调整读取内容的方法

f = open('D:/桌面文件/Python/新建文本文档.txt','r')

print('读取固定文字长度')
print(f.read(20))
f.seek(0)

print( '读取一行')
print(f.readline())
f.seek(0)

print( '读取一行中的前XX个字符')
print(f.readline(40))
f.seek(0)

print( '将文件按行生成字符串列表')
print(f.readlines())
f.seek(0)

print( '遍历每一行的方法')
for line in f.readlines():
    print(line)
f.seek(0)

输出内容----------------------
读取固定文字长度
Beautiful is better
读取一行
Beautiful is better than ugly.

读取一行中的前XX个字符
Beautiful is better than ugly.

将文件按行生成字符串列表
['Beautiful is better than ugly.\n', 'Explicit is better than implicit.\n', 'Simple is better than complex.\n', 'Complex is better than complicated.\n', 'Flat is better than nested.\n', 'Sparse is better than dense.\n', 'Readability counts.\n', "Special cases aren't special enough to break the rules.\n", 'Although practicality beats purity.\n', 'Errors should never pass silently.\n', 'Unless explicitly silenced.\n', 'In the face of ambiguity, refuse the temptation to guess.\n', 'There should be one-- and preferably only one --obvious way to do it.\n', "Although that way may not be obvious at first unless you're Dutch.\n", 'Now is better than never.\n', 'Although never is often better than *right* now.\n', "If the implementation is hard to explain, it's a bad idea.\n", 'If the implementation is easy to explain, it may be a good idea.\n', "Namespaces are one honking great idea -- let's do more of those!"]
遍历每一行的方法
Beautiful is better than ugly.

Explicit is better than implicit.

Simple is better than complex.

Complex is better than complicated.

Flat is better than nested.

Sparse is better than dense.

Readability counts.

Special cases aren't special enough to break the rules.

Although practicality beats purity.

Errors should never pass silently.

Unless explicitly silenced.

In the face of ambiguity, refuse the temptation to guess.

There should be one-- and preferably only one --obvious way to do it.

Although that way may not be obvious at first unless you're Dutch.

Now is better than never.

Although never is often better than *right* now.

If the implementation is hard to explain, it's a bad idea.

If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!
# import this


# 关于路径的书写方式
path1 = "D:\\桌面文件\\Python\\test.txt"
path2 = "D:/桌面文件/Python/test.txt"
path3 = r"D:\桌面文件\Python\test.txt"

print(path1,path2,path3)

# 读取文件: open语句

f = open(path1,'r',encoding='utf8')
print(type(f))
print(f)
print(f.read)
print('读取完毕')

'''
open (”路径“,”模式“,encoding = ”编码“)
模式:   
        r:读取文件,默认;   
        w:写入
        rw:读取+写入
        a:追加

简单的读取犯法: .read()   读取后,光标会在末尾
'''

print("二次读取")
print(f.read)
print("二次读取完毕")
# 运行第一次.read()之后,光标位于末尾,再次读取输出为空

f.seek(0)
print("三次读取")
print(f.read)
print("三次读取完毕")
# 所以现在用 f.seek(0) 来移动光标

f.close())
print("四次读取")
print(f.read)
print("四次读取完毕")
# 关闭文件连接  f.close(),养成一个好习惯
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值