用python与文件进行交互的方法


Posted in Python onMarch 01, 2018

本文介绍了用python与文件进行交互的方法,分享给大家,具体如下:

一.文件处理

1.介绍

计算机系统:计算机硬件,操作系统,应用程序

应用程序无法直接操作硬件,通过操作系统来操作文件,进而读/写硬件中的文件。

python打开文件过程:

#打开
f=open('a.txt','r')
#通过句柄对文件进行操作
read_f=f.read()
#关闭文件
f.close()
with open('a.txt','r') as f:  #不需要关闭
f.close() #回收操作系统打开的文件
del f #回收应用程序级的变量

2.打开文件的模式

a.打开文本文件

#r,只读模式【默认模式,文件必须存在,不存在则抛出异常】
f=open('a.txt',encoding='utf-8')
data1=f.read()
print(f.readline(),end='')
print(f.readlines())
#w,只写模式【不可读;不存在则创建;存在则清空内容】
f=open('a.txt','w',encoding='utf-8')
f.write('werf')
#a,只追加写模式【不可读;不存在则创建;存在则只追加内容】
f=open('a.txt','a',encoding='utf-8')
f.write('werf\n')

b.对于非文本文件,只能使用b模式,"b"表示以字节的方式操作(而所有文件也都是以字节的形式存储的,使用这种模式无需考虑文本文件的字符编码、图片文件的jgp格式、视频文件的avi格式

with open('1.jpg','rb') as f_read:
  data=f_read.read()
  print(data)
with open('a.txt','rb') as f_read:
  data=f_read.read().decode('utf-8') #解码
  print(data)
with open('a.txt','wb')as f_write:
  f_write.write('adsf'.encode('utf-8'))
'''
练习,利用b模式,编写一个cp工具,要求如下:

1. 既可以拷贝文本又可以拷贝视频,图片等文件


2. 用户一旦参数错误,打印命令的正确使用方法,如usage: cp source_file target_file
'''
import sys
if len(sys.argv)!=3:
  print('usage:cp source_file target_file')
  sys.exit()
source_file,target_file=sys.argv[1],sys.argv[2]
print()
with open(source_file,'rb')as f_read,open(target_file,'wb')as f_write:
  for line in f_read:
    f_write.write(line)

3.文件内光标的移动

#以文本模式读文件,n代表的是字符的个数
with open('a.txt','r')as f_read:
  data=f_read.read(6)
  print(data)
#以b模式读文件,n代表的是字节的个数
with open('a.txt','rb')as f_read:
  data=f_read.read(6)
  print(data)
# tell:告诉当前光标的位置
with open('a.txt','r',encoding='utf-8')as f_read:
  data=f_read.read(4)
  data1=f_read.tell()
  print(data,data1)
# seek:移动光标(0:文件开头默认;1:文件当前光标;2:文件末尾)
with open('a.txt', 'r', encoding='utf-8')as f_read:
  data = f_read.seek(3)
  data1 = f_read.read()
  print(data, data1)
# 实现tail功能
import time
with open('access.log', 'rb')as f_read:
  f_read.seek(0,2)
  while True:
    line = f_read.readline()
    if line:
      print(line.decode('utf-8'),end='')
    else:
      time.sleep(1)

4.文件的修改

import os

with open('a.txt') as read_f,open('.a.txt.swap','w') as write_f:
  for line in read_f:
    line=line.replace('alex','SB')
    write_f.write(line)

os.remove('a.txt')
os.rename('.a.txt.swap','a.txt')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python3基础之输入和输出实例分析
Aug 18 Python
Python中optparse模块使用浅析
Jan 01 Python
python通过字典dict判断指定键值是否存在的方法
Mar 21 Python
Python中splitlines()方法的使用简介
May 20 Python
Python机器学习之决策树算法
Dec 22 Python
Python下载网络文本数据到本地内存的四种实现方法示例
Feb 05 Python
Python OpenCV获取视频的方法
Feb 28 Python
Python实现求解括号匹配问题的方法
Apr 17 Python
Python返回数组/List长度的实例
Jun 23 Python
Cython编译python为so 代码加密示例
Dec 23 Python
Python控制台输出时刷新当前行内容而不是输出新行的实现
Feb 21 Python
python装饰器代码解析
Mar 23 Python
python爬虫爬取快手视频多线程下载功能
Feb 28 #Python
python爬取m3u8连接的视频
Feb 28 #Python
python实现m3u8格式转换为mp4视频格式
Feb 28 #Python
浅谈Python中的私有变量
Feb 28 #Python
python中logging包的使用总结
Feb 28 #Python
深入理解Python爬虫代理池服务
Feb 28 #Python
python实现装饰器、描述符
Feb 28 #Python
You might like
PHP设计模式之责任链模式的深入解析
2013/06/13 PHP
PHP基于phpqrcode生成带LOGO图像的二维码实例
2015/07/10 PHP
利用PHP如何写APP接口详解
2016/08/23 PHP
PHP7基于curl实现的上传图片功能
2018/05/11 PHP
PHP7数组的底层实现示例
2019/08/25 PHP
php 比较获取两个数组相同和不同元素的例子(交集和差集)
2019/10/18 PHP
PHP 实现重载
2021/03/09 PHP
JScript中的"this"关键字使用方式补充材料
2007/03/08 Javascript
jquery Ajax 实现加载数据前动画效果的示例代码
2014/02/07 Javascript
addEventListener 的用法示例介绍
2014/05/07 Javascript
浅谈JavaScript中null和undefined
2015/07/09 Javascript
详解react关于事件绑定this的四种方式
2018/03/09 Javascript
解决低版本的浏览器不支持es6的import问题
2018/03/09 Javascript
深入理解javascript prototype的相关知识
2019/09/19 Javascript
Object.keys() 和 Object.getOwnPropertyNames() 的区别详解
2020/05/21 Javascript
[04:40]2016国际邀请赛中国区预选赛全程TOP10镜头集锦
2016/07/01 DOTA
[06:49]2018DOTA2国际邀请赛寻真——VirtusPro傲视群雄
2018/08/12 DOTA
详解详解Python中writelines()方法的使用
2015/05/25 Python
深入分析python中整型不会溢出问题
2018/06/18 Python
Python延时操作实现方法示例
2018/08/14 Python
python按时间排序目录下的文件实现方法
2018/10/17 Python
利用python和百度地图API实现数据地图标注的方法
2019/05/13 Python
python实现视频分帧效果
2019/05/31 Python
Python打印特殊符号及对应编码解析
2020/05/07 Python
python将logging模块封装成单独模块并实现动态切换Level方式
2020/05/12 Python
带你学习Python如何实现回归树模型
2020/07/16 Python
Python通过getattr函数获取对象的属性值
2020/10/16 Python
Pandas之缺失数据的实现
2021/01/06 Python
异常和异常类的概念
2014/09/12 面试题
三年级科学教学反思
2014/01/29 职场文书
物理教学随笔感言
2014/02/22 职场文书
监察建议书范文
2014/03/12 职场文书
医药公司采购员岗位职责
2014/09/12 职场文书
个人查摆问题及整改措施
2014/10/16 职场文书
2014年销售人员工作总结
2014/11/27 职场文书
2014年城市管理工作总结
2014/12/02 职场文书