文件(file)操作
文件操作:
1.打开文件,得到文件句柄并赋值给一个变量
2.通过句柄对文件进行操作
3.关闭文件
#能调用方法的一定是对象
# li = [1,2,3]
# li.append('22')#列表对象
# 'asc'.capitalize()#字符串对象
# 昨夜寒蛩不住鸣。
# 惊回千里梦,已三更。
# 起来独自绕阶行。
# 人悄悄,帘外月胧明。
# 白首为功名,旧山松竹老,阻归程。
# 欲将心事付瑶琴。
# 知音少,弦断有谁听。
文件打开方式:
'r' open for reading (default)
'w' open for writing, truncating the file first
'x' create a new file and open it for writing
'a' open for writing, appending to the end of the file if it exists
'b' binary mode
't' text mode (default)
'+' open a disk file for updating (reading and writing)
'U' universal newline mode (deprecated)
# f = open('小重山','r',encoding='utf8')
# data = f.read()
# #data = f.read(5) 读出前5个字符
# print(data)
#
# f.close()
# #f = open('小重山','w',encoding='utf8')#创建此对象时清空文件内容
# f = open('小重山2','w',encoding='utf8')#没有文件,先创建
# print(f.fileno())句柄
#
# f.write('hello world\n')
# f.write('冷大爷')
# f.close()
import time
f = open('小重山2','a',encoding='utf8')#a --- append
f.write('\n hello world \n')#放入缓冲区
f.write('冷大爷')
#time.sleep(50)
f.close()#写入磁盘,关闭文件
# f = open('小重山2','r',encoding='utf8')
# print(f.readline())
# print(f.readline())
# # readline(self, limit: int = -1) -> AnyStr:
# # pass
# print(f.readlines())#转换成列表打印
# # readlines(self, hint: int = -1) -> List[AnyStr]:
# # pass
# number = 0
# for i in f.readlines():
# number += 1
# if number == 6:
# # # print(i.strip(),'i like it')
# # else:
# # print(i.strip())
# ##i.strip() + 'i like it'
# i = ' '.join((i.strip(),"123456"))#字符串拼接用jion
# print(i.strip())
# f.close()
#
#
# num = 0
# for i in f: #这是for内部将f对象做成一个迭代器,用一行去一行
# num += 1
# if num == 6:
# i = ''.join([i.strip(),'jkl'])
#
# print(i.strip())
# f.close()
# print(f.tell())#指出光标位置
# print(f.read(4))
# print(f.tell())
#
# f.seek(0)#调整光标位置
# print(f.read(4))
# import sys,time #进度条效果
# for i in range(30):
# sys.stdout.write('*')
# sys.stdout.flush()
# time.sleep(0.1)
#
# # def flush(self, *args, **kwargs): # real signature unknown
# # """
# # Finish the compression process.
# #
# # Returns the compressed data left in internal buffers.
# #
# # The compressor object may not be used after this method is called.
# # """
# # pass
# f = open('小重山2','w',encoding='utf8')
# f.truncate()
# # truncate(self, size: int = None) -> int:
# f.close()
# #r+模式,w+模式,a+模式(可读)
#r+ 可读可写 只能在最后写 读时从头读
# f = open('小重山2','r+',encoding='utf8')
# print(f.readline())
# f.write('岳飞')
# f.close()
# #w+ 清空后再写 写后光标在最后
# f = open('小重山2','w+',encoding='utf8')
#
# f.write('岳飞')
# print(f.tell())#6----光标在‘岳飞’后面
# f.seek(0)
# print(f.readline())
# f.close()
#a+ 在最后添加 从最后读
#怎么修改文件?新建文件,复制
#错误
# f = open('小重山2','r+',encoding='utf8')
# num = 0
# for line in f:
# num += 1
# if num == 6:
# f.write('Jayce')#r w 光标不一致 r从头读 w从最后写
# f.close()
# f_read = open('小重山','r',encoding='utf8')
# f_write = open('小重山2','w',encoding = 'utf8')
# number = 0
# for line in f_read:
# number += 1
# if number == 5:
# line = ''.join([line.strip(),'岳飞\n'])
# #line = 'hello 岳飞\n'
# f_write.write(line)
#
# f_read.close()
#作业涉及方法
# a = str({'beijing':{'1':123}})
# print(type(a))
# print(a) #{'beijing': {'1': 123}}
# a = eval(a)
'''
eval()文档
eval(*args, **kwargs): # real signature unknown
"""
Evaluate the given source in the context of globals and locals.
The source may be a string representing a Python expression
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mapping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.
"""
'''
# print(type(a))
# print(a['beijing'])
#with
f = open('log','r')
f.readline()
f.read()
f.close()
#与以下等价 with无须再close
with open('log','r') as f:
f.readline()
f.read()
print('冷大爷')
#with同时对两个句柄进行操作
with open('log1','r',encoding='utf8') as f_read , open('log2','w',encoding='utf8') as f_write :
for line in f_read:
f_write.write(line)
转载于:https://www.cnblogs.com/lengyao888/p/10445156.html
文件(file)操作相关推荐
- Java文件File操作一:文件的创建和删除
一.简述 File 文件类,主要对文件进行相关操作.常用的File操作有:文件(夹)的创建.文件(夹)的删除,文件的读入和下载(复制)等: 二.文件(夹)的创建和删除 1.创建过程 实例: //cre ...
- go语言中的文件file操作
一.File文件操作 首先,file类是在os包中的,封装了底层的文件描述符和相关信息,同时封装了Read和Write的实现. 1.FileInfo接口 FileInfo接口中定义了File信息相关的 ...
- Python——文件(File)操作汇总
文章目录 写在前面 1. Python文件的打开.读写.关闭 1.1 第一步:打开文件--open() 1.2 第二步:从文件中读取/写入数据 1.2.1 读数据 1.2.1.1 read() 1.2 ...
- android java file 清理垃圾获取文件大小 删除文件等操作
这么久没有写博客了,今天给大家分享一些多file文件的操作.一般可以用到清理垃圾获取文件大小 删除文件等操作,可以直接用于工具类里面,直接做操作便可以 public final class FileU ...
- File类对文件的操作应用
1.在不存在的文件夹下创建文件 //在当前模块下aaa文件下ddd下eee中创建一个e.txt文件 public class Demo2 {public static void main(String ...
- python文件输入符_python文件IO与file操作
1 标准输入输出IO - (1) 打印到屏幕 print() print(self, *args, sep=' ', end='n', file=None): 把传递的表达式 转换成一个 字符串表达式 ...
- android 获取文件夹的字节数,android java file 清理垃圾获取文件大小 删除文件等操作...
这么久没有写博客了,今天给大家分享一些多file文件的操作.一般可以用到清理垃圾获取文件大小 删除文件等操作,可以直接用于工具类里面,直接做操作便可以 public final class FileU ...
- .NET基础-11-ArrayList|Hashtable|File文件操作|Dircetioy文件夹操作|Path路径操作
集合 ArrayList与Hashtable应为存在拆箱与装箱,所以性能不怎么好,尽量不要使用,而使用泛型集合 可以使用下面的方式输出所消耗的时间 //ArrayList arl = new Arra ...
- Go语言自学系列 | golang标准库os模块 - File文件读操作
视频来源:B站<golang入门到项目实战 [2021最新Go语言教程,没有废话,纯干货!持续更新中...]> 一边学习一边整理老师的课程内容及试验笔记,并与大家分享,侵权即删,谢谢支持! ...
- 文件及文件夹操作- File类、Directory 类、FileInfo 类、DirectoryInfo 类
命名空间:using system .IO; 1. File类: 创建:File.Create(路径);创建文件,返回FileStream FileStream fs = File.Create(路径 ...
最新文章
- 又叒叕是一篇讲缓存的文章
- 着手一个手游项目的思考
- 关于Servlet中filter过滤器的小问题
- 开发者友好性和易用性
- vue项目启动出现cannot GET /服务错误
- L1-045 宇宙无敌大招呼-PAT团体程序设计天梯赛GPLT
- 诹图系列(2): 堆积条形图
- 将java项目打包为jar
- 嵌入式操作系统风云录:历史演进与物联网未来.
- 为什么我的世界服务器显示红叉,我的世界藏宝图怎么看红叉
- 某大厂算法工程师面试题详解,问题+答案
- 阿里短信单发,批量发送
- 人生之路 — 开启智慧之脑
- 天啦,从Mongo到ClickHouse我到底经历了什么?
- Git常用命令及方法大全
- PS2018学习笔记(03-18节)
- IAR打开软件,很多文件找不到
- uni-app - 监听用户滚动屏幕开始与结束(解决方案)
- 使用ICSharpCode.SharpZipLib对文件进行压缩或解压
- Netty(一)基础socketchannel,Buffer,selector黏包 半包解决 实战
热门文章
- ·2010考研数学二第(19)题——多元微分学:复合函数求偏导、链式法则
- css如何选择相同class下的第一个class元素和最后一个元素?
- MPU和MMU、MPU和MCU的区别
- R、RStudio下载与安装方法
- 怎么安装aptdaemon模块_安装Pulseaudio模块在Ubuntu中开启蓝牙APTX/LDAC支持
- GIthub上关于新冠肺炎数据整理的项目汇总
- windows7、windows10 桌面快捷方式左下角有一个白色方块
- C++ ARX二次开发视图
- 数据库:数据查询(指定的列、全部列、经过计算的值、消除重复的行、查询满足条件的元组、比较查询、范围查询、集合查询、字符匹配查询)
- Mysql性能衡量指标