file的基本操作;file的修改
file的基本操作
# Author:nadech
# 文件读写/修改/
#data = open("yesterday",encoding="utf-8").read()
#这里要设置打开模式,默认是读r;
#w的时候会打开文件,把之前文件覆盖;
#a模式,append追加
import sys,time f = open("yesterday","r",encoding="utf-8")#f叫文件句柄,它包含了文件的名称,文件的大小,文件内存的起始位置等 #改变文件句柄的指针位置
print(f.readline())
print(f.tell())
f.seek(0)
print(f.readline())
#编码方式
print(f.encoding)
#操作系统给该文件句柄分配的编号
print(f.fileno())
# flush(),文件在执行写操作时,并不是实时写入到硬盘中的,而是当内存中缓存到一定数量之后写入,但是有些数据要求实时写入就可以用flush()强制写入 data = f.read()
print(data) f.write("\n 我爱北京天安门,\n")
f.write("\n 你是一个大傻逼。") print(f.readline())
print(f.readline()) #readlines只适合读小文件
for line in f.readlines():
print(line.strip()) #只读5行
for index,line in enumerate(f.readlines()):
print("%s:"%(index),line) # 以上读文件的方法都可以忘记,因为上边的效率很低
# 这个方法是读入内存中一行,执行一行,而不是所有的都读到内存中
# 但是这个需要自己加一个计数器 count = 0
for line in f:
if count == 9:
print("——————我是分割线——————")
count+=1 print(line.strip())
count+=1
for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.2)
#truncate(),截断,里边的数字是从头剩下的字符数
f.truncate() #读写,可以读,可以写,写在后边
f1 = open("yesterday2","r+",encoding="utf-8") print(f1.readline())
print(f1.readline())
print(f1.readline()) f1.write("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
f1.write("gkukhkhkhkhkh")
#写读 w+
#追加读 a+,这两个模式不怎么使用 #rb,用来读二进制文件,但是并不是看到的01010101,是文件按照二进制处理(网络传输ftp,socket;视频文件,音频文件)
# f = open("yesterday","rb")
#wb,二进制的写 #with语句 #with open("yesterday","r",encoding="utf-8") as f:
#同时打开多个文件0
# with open("yesterday","r",encoding="utf-8"),\
# open("yesterday","r",encoding="utf-8")
file的修改并不是直接在源文件中进行修改,而是新建文件,将要修改的内容修改掉
# Author:nadech
f = open("yesterday","r",encoding="utf-8")
f_new = open("yesterday_back","w",encoding="utf-8")
for line in f:
if "令我笑容满面" in line:
line = line.replace("令我笑容满面","令nadech笑容满面")
f_new.write(line)
file的基本操作;file的修改的更多相关文章
- Java File类基本操作
我们可以利用Java.io.File类对文件进行操作,基本操作如下: 1)创建文件: public boolean createNewFile() throws IOException 2)删除文件: ...
- 01.File文件基本操作
1-创建File对象 /** * 创建我们 java.io.File对象 */ public static void test1() { //第一创建对象方式 File parent=new File ...
- File 的基本操作
package xinhuiji_day07; import java.io.File;import java.io.IOException; public class FileTest { /** ...
- linux 缺少动态连接库.so--cannot open shared object file: No such file or directory
error while loading shared libraries的解決方法 执行行程式時,如此遇到像下列這種錯誤: ./tests: error while loading shared l ...
- 【Android】error opening trace file: No such file or directory (2)
1.问题描述: 运行报错: 12-25 13:35:32.286: E/Trace(1202): error opening trace file: No such file or directory ...
- 关于ImportError: libssl.so.10: cannot open shared object file: No such file or directory unable to load app 0 (mountpoint='') (callable not found or import error)
一.问题描述 在亚马逊云服务器使用Nginx+uwsgi部署django项目时,项目可以使用python manage.py runserver正常运行,uwsgi测试也没问题,Nginx也正常启动, ...
- this inspection reports usage of the default file template for file header
使用idea创建一个java class的时候会出现如下的warning: this inspection reports usage of the default file template for ...
- nginx排错error while loading shared libraries:libpcre.so.1:cannot open shared object file:No such file or directory
启动nginx报错:error while loading shared libraries:libpcre.so.1:cannot open shared object file:No such f ...
- centos6.9安装xampp后报错:egrep: error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory
1.centos6.9安装xampp(xampp-linux-x64-7.0.21-0-installer.run)后启动的时候,报错: egrep: error while loading shar ...
随机推荐
- 收藏:视频网站(JavaEE+FFmpeg)/Nginx+ffmpeg实现流媒体直播点播系统
FFmpeg安装(windows环境)http://www.cnblogs.com/xiezhidong/p/6924775.html 最简单的视频网站(JavaEE+FFmpeg)http://bl ...
- JQuery开发报错集锦
报错一:JQuery $.each遍历JSON字符串报Uncaught TypeError:Cannot use 'in' operator to search for "70". ...
- 模拟Paxos算法及其简单学习总结
一.导读 Paxos算法的流程本身不算很难,但是其推导过程和证明比较难懂.在Paxos Made Simple[1]中虽然也用了尽量简化的流程来解释该算法,但其实还是比较抽象,而且有一些细节问题没有交 ...
- MySQL 5.7 基于复制线程SQL_Thread加快恢复的尝试
1. MySQL 数据恢复常用办法 MySQL恢复的方法一般有三种: 1. 官方推荐的基于全备+binlog , 通常做法是先恢复最近一次的全备,然后通过mysqlbiinlog --start-po ...
- 【Android】Mac下Android Studio设置App启动页
先将启动页放到项目资源中,图片一般是1080*1920的jpg. 新建一个activity,如图: 创建成功之后,打开刚刚创建的activity,来进行代码的编写: public class BZLa ...
- 0307-关于html
html最主要的三点: 1.标签的写法.用法 <标签名 属性名1="属性值1" 属性名2="属性值2">内容</标签名> 比如:< ...
- springMvc配置xml使ResponseBody返回Json
@ResponseBody 在返回的数据不是html标签的页面,而是其他某种格式的数据时(如json.xml等)使用: 不在springMvc中配置json的处理的话,我们通常会在Controller ...
- [LeetCode] Employee Importance 员工重要度
You are given a data structure of employee information, which includes the employee's unique id, his ...
- ES6(解构赋值)
解构赋值 1.什么是解构赋值? 在语法上,就是赋值的作用,解构为(左边一种解构.右边一种解构,左右一一对应进入赋值) 2.解构赋值的分类. 1.左右为数组即为数组解构赋值:2.左右为对象即为对象解构赋 ...
- 【实验吧】CTF_Web_简单的SQL注入之1
题目链接:http://ctf5.shiyanbar.com/423/web/ 简单的SQL注入之1,比2,3都简单一些.利用2 的查询语句也可以实现:1'/**/union/**/select/** ...