python3.7 文件操作
#!/usr/bin/env python
__author__ = "lrtao2010" #python3.7 文件操作 # r 只读,默认打开方式,当文件不存在时会报错
# w 只写,当文件不存在时会自动创建文件,文件内容只能是字符串,只能写入字符串
# r+ 可读可写,当文件不存在时会报错
# w+ 可读可写。当文件不存在时会新建
# a 追加文件,不可读
# a+ 追加文件,可读可写
# rb 以二进制读模式打开,只可读
# rb+ 以二进制写读写模式打开,可读可写,当文件不存在时报错
# wb 以位进制写模式打开,只可写
# wb+ 以二进制读写模式打开,可读可写。当文件不存在时新建
# ab 以二进制追加模式打开,追加文件,不可读
# ab+ 以二进制读写模式打开,追加文件。可读可写 # with open("file.txt",'w',encoding='utf-8') as f:
# print(f.readable())
# print(f.writable())
# False
# True
# with open("file.txt",'r',encoding='utf-8') as f:
# print(f.readable())
# print(f.writable())
# True
# False
# with open("file.txt",'r+',encoding='utf-8') as f:
# print(f.readable())
# print(f.writable())
# True
# True
# with open("file.txt",'a',encoding='utf-8') as f:
# print(f.readable())
# print(f.writable())
# False
# True
# with open("file.txt",'a+',encoding='utf-8') as f:
# print(f.readable())
# print(f.writable())
# True
# True # f= open("file1.txt",encoding='utf-8')#不写encoding,默认问操作系统编码
# file_data=f.read() #完全读取
# print(file_data)
# f.close() #需要执行关闭操作
#
#
#
#
#
#
# 你好!
# hello # with open("file.txt",encoding='utf-8')as f:
# file_data=f.read()
# print(file_data) #不需要执行close(),系统会自动关闭。
#
#
#
#
#
#
# 你好!
# hello # with open("file.txt",encoding='utf-8')as f:
# print(f.readable()) #判断是否可读
# print(f.writable()) #判断是否可写
# print(1, f.readline()) #一次只读一行
# print(2, f.readline())
# print(3, f.readline())
# print(4, f.readline())
# print(5, f.readline())
# print(6, f.readline())
# print(7, f.readline())
# print(8, f.readline())
# True
# False
# 1 1111
#
# 2 222
#
# 3 333
#
# 4 44
#
# 5 5555
#
# 6 666
#
# 7 你好!
#
# 8 hello #with open("file.txt",encoding='utf-8')as f:
# print(f.readlines()) #将文件内容读取到列表中
# ['1111\n', '222\n', '333\n', '44\n', '5555\n', '666\n', '你好!\n', 'hello']
# for i in f.readlines():
# print(i)
#
#
#
#
#
#
#
#
#
#
#
#
# 你好!
#
# hello # with open('file.txt','r',encoding='utf-8',newline='') as f:
# # print(f.readlines())
# # ['1111\r\n', '222\r\n', '333\r\n', '44\r\n', '5555\r\n', '666\r\n', '你好!\r\n', 'hello']
# for i in f.readlines():
# print(i,end='') #不打印换行符
#
#
#
#
#
#
# 你好!
# hello # with open('file.txt','w+',encoding='utf-8') as f: #原文件被清空后重新写入
# f.write('a\n')
# f.write('b\nc\n')
# f.writelines(['d\n', 'e\n'])
# f.seek(0) #将指针seek到0位置,否则读不出数据
# print(f.read())
# # a
# # b
# # c
# # d
# # e # with open('file.txt','r+',encoding='utf-8') as f: #从指针位置所在处写入写入
# print(f.readline())
# f.write('a\n')
# f.write('b\nc\n')
# f.writelines(['d\n', 'e\n'])
# f.seek(0) #将指针seek到0位置,否则读不出数据
# print(f.read())
# # 1111
# #
# # 1111
# # 222
# # 333
# # 44
# # 5555
# # 666
# # 你好!
# # helloa
# # b
# # c
# # d
# # e # with open('file.txt','a+',encoding='utf-8') as f:
# f.write('写到文件最后')
# f.seek(0)
# print(f.read())
# # 1111
# # 222
# # 333
# # 44
# # 5555
# # 666
# # 你好!
# # hello写到文件最后 # with open('file.txt','a+',encoding='utf-8') as f:
# print(f.encoding) #查看文件编码
# #utf - 8 # #'字符串'---------encode---------》bytes
# #bytes---------decode---------》'字符串' # with open('file.txt','rb') as f: #b模式不能指定编码
# file_data=f.read()
# print(file_data)
# print(file_data.decode('utf-8'))
# # b'1111\r\n222\r\n333\r\n44\r\n5555\r\n666\r\n\xe4\xbd\xa0\xe5\xa5\xbd!\r\nhello'
# # 1111
# # 222
# # 333
# # 44
# # 5555
# # 666
# # 你好!
# # hello # with open('file.txt','wb+') as f:
# file_data = 'test wb'
# f.write(file_data.encode('utf-8'))
# f.seek(0)
# print(f.read())
# # b'test wb' # flush() 文件内容从内存刷到硬盘
# tell() 查看文件当前光标位置
# seek(3) #从开头开始算,将光标移动到第三个字节
#seek 有三种工作方式,seek(offset[, whence])
#seek(2,0)=seek(2),0是默认方式,相当于从0字节位置开始
#seek(2,1) 1 相对当前位置
#seek(-2,2) 2 从文件末尾开始
# truncate(10) #从开头开始算,将文件只保留从0-10个字节的内容,文件打开方式必须包含"写",
#但是w和w+除外,因为这两种方式会首先把文件清空。
# with open('file.txt','ab') as f:
# f.truncate(1) #打印文件最后一行
# with open("file.txt",'rb') as f:
# for i in f: #这种方式不会读取整个文件,需要从哪里读取才从哪里开始读取,循环文件的推荐方式
# offs=-5 #偏移量,根据一行大小确定
# while True:
# f.seek(offs,2)
# data=f.readlines()
# if len(data) > 1:
# print('这是最后一行:',data[-1].decode('utf-8'))
# break
# offs*=2 # 这是最后一行: hello你好!hello你好!hello你好!hello你好!
python3.7 文件操作的更多相关文章
- 【python3之文件操作】
一.文件操作 1.文件处理的流程 1)打开文件,得到文件句柄并赋值给一个变量 2)通过句柄对文件进行操作 3)关闭文件 例如: f = open('chenli.txt') #打开文件 first_l ...
- (17)-Python3之--文件操作
1.文件的操作流程 第一,建立文件对象. 第二,调用文件方法进行操作. 第三,不要忘了关闭文件.(文件不关闭的情况下,内容会放在缓存,虽然Python会在最后自动把内容读到磁盘,但为了以防万一,要养成 ...
- python3的文件操作
open的原型定义在bultin.py中,是一种内建函数,用于处理文件 open(file, mode='r', buffering=None, encoding=None, errors=None, ...
- python3之文件操作
一 打开文件 根目录在d盘的文件名为‘学习资料.txt’的文件 a)绝对路径(最开始的,根目录文件)例: e:\学习资料.txt 相对路径 直接用文件名字 b)操作方式 只读 只 ...
- python3中文件操作及编码
#之前一直没明白文件处理中的w和wb的区别到底是什么,#在看过视频后才知道,原来在linux里面是没有区别的,#但是在windows里面就能够看出区别来了#下面来个例子: with open(&quo ...
- python3中文件/IO编程
python3的文件操作可谓是我见过所有语言中最舒服的,那我们来一起看一下py3中的文件操作. 1:文件的打开方式有以下几种: 注:以上图表参考菜鸟教程 2:定位读写文件 f = open(&quo ...
- Python3学习之路~2.7 文件操作
对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 Somehow, it seems the love I knew was always the ...
- Python3 文件操作(十六)
一 文件操作 1.介绍 计算机系统分为:计算机硬件,操作系统,应用程序三部分. 我们用python或其他语言编写的应用程序若想要把数据永久保存下来,必须要保存于硬盘中,这就涉及到应用程序要操作硬件,众 ...
- Python3.x:open()文件操作
Python3.x:open()文件操作 open/文件操作: #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 f=ope ...
随机推荐
- Codeforces Round #527-B. Teams Forming(贪心)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- PHP全国省市区地址分割提取脚本程序
github地址: https://github.com/zmxfree/addressapart 比如将 浙江省杭州市江干区XX路X号 分割成 浙江省 杭州市 江干区 XX路X号,方便excel操作 ...
- 转 Oracle 11g Rman – 08317错误
在一次帮助客户解决归档满的过程中遭遇了此错误. 客户是新上线系统,11g版本.设置了归档清除脚本(脚本参考:http://www.ludatou.com/?p=766),结果发现以往没问题的脚本在此刻 ...
- 077 Combinations 组合
给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合.例如,如果 n = 4 和 k = 2,组合如下:[ [2,4], [3,4], [2,3], [1,2], [ ...
- stm32f107的使用:
一 不能支持软件仿真: 二 外部晶体推荐25MHZ,但如果不用音频接口,也可以使用8M晶体,需修改这里成8000000: 此时设置如下: 并修改这里 改为: 因为
- MDX分页查询
WITH SET [e16a30d0-2174-4874-8dae-a5085a75a3e2] as NONEMPTY({[Measures].[终端销售数量], [Measures].[终端销售吊牌 ...
- 洛谷P1057 传球游戏
f[i][j]表示第i轮j拿到球的方案数 转移:f[i][j]=f[i-1][j+1] +f[i-1][j+-1].注意: 边界f[0][1]=1; 还有当j=1或N时 #include<ios ...
- gulp管理angular2项目 配置文件
目录结构: projectName |_ src |_ app |_ index.html |_ main.ts |_ systemjs.config.js |_ gulpfile.js |_ pac ...
- 2017年3月14日-----------乱码新手自学.net 之Authorize特性与Forms身份验证(登陆验证、授权小实例)
有段时间没写博客了,最近工作比较忙,能敲代码的时间也不多. 我一直有一个想法,想给单位免费做点小软件,一切思路都想好了,但是卡在一个非常基础的问题上:登陆与授权. 为此,我看了很多关于微软提供的Ide ...
- 如何构建多模块的SpringBoot项目
通过阅读本文你将了解到:如何将已有SpringBoot项目改成多模块 & 如何新构建多模块SpringBoot项目 以下示例基于我正在使用的order(订单服务)进行演示,无论你用的是什么项目 ...