1:只读(‘r' 和 ’rb'以字节读)

f = open('d:\模特主妇护士班主任.txt',mode='r',encoding='UTF-8')
content = f.read()
print(content)
f.close()

相对路径

 f = open('模特主妇护士班主任',mode='r',encoding='utf-8')
content = f.read()
f.close() 若以rb模式,则不需encoding=.......

2:读写('r+'和‘r+b'以bytes字节读写)

 f = open('log',mode='r+',encoding='utf-8')
print(f.read())
f.write('大猛,小孟')
f.close() f = open('log',mode='r+b')
print(f.read())
f.write('大猛,小孟'.encode('utf-8'))
f.close()

3:只写('w'和'wb')

先将源文件的内容全部清除,再写。

 f = open('log',mode='w',encoding='utf-8')
f.write('附近看到类似纠纷')
f.close() f = open('log',mode='wb')
f.write('附近看到类似纠纷'.encode('utf-8'))
f.close()

4:写读

w+ , w+b

 f = open('log',mode='w+',encoding='utf-8')
f.write('aaa')
f.seek(0)
print(f.read())
f.close()

5:追加 'a'

f = open('log',mode='a',encoding='utf-8')
f.write('佳琪')
f.close() f = open('log',mode='ab')
f.write('佳琪'.encode('utf-8'))
f.close() 注意:seek()--以字符定位光标位置,一个中文
字体代表3个字符,一个英文字母代表一个字符

6:文件操作其他功能

# obj = open('log',mode='r+',encoding='utf-8')
# content = f.read(3) # 读出来的都是字符
# f.seek(3) # 是按照字节定光标的位置
# f.tell() 告诉你光标的位置
# print(f.tell())
# content = f.read()
# print(content)
# f.tell()
# f.readable() # 是否刻度
# line = f.readline() # 一行一行的读
# line = f.readlines() # 每一行当成列表中的一个元素,添加到list中
# f.truncate(4)
# for line in f:
# print(line)
# f.close() # f = open('log',mode='a+',encoding='utf-8')
# f.write('佳琪')
# count = f.tell()
# f.seek(count-9)
# print(f.read(2))
# f.close() # with open('log',mode='r+',encoding='utf-8') as f,\
# open('log',mode='w+',encoding='utf-8') as f1:

File operations 1的更多相关文章

  1. python file operations

    原文地址 总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当 ...

  2. File Operations

    在刷题测试程序时,为了避免每次都手工输入,我们可以把输入数据保存在文件中:为了避免输出太长,我们将输出也写入文件中,方便与标准答案文件进行比较. 文件使用一般有两种方法:输入输出重定向.fopen. ...

  3. backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

    昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx> ...

  4. VMWare File Format Learning && Use VHD File To Boot VMWare && CoreOS Docker Configuration And Running

    目录 . Virtual Machine Introduce . Vmware Image File Format . VHD File Format . Convert VHD File Into ...

  5. Python File I/O

    File is a named location on disk to store related information. It is used to permanently store data ...

  6. File Operation using SHFileOperation

    SHFILEOPSTRUCT Original link: http://winapi.freetechsecrets.com/win32/WIN32SHFILEOPSTRUCT.htm Refere ...

  7. python文件和文件夹訪问File and Directory Access

    http://blog.csdn.net/pipisorry/article/details/47907589 os.path - Common pathname manipulations 都是和路 ...

  8. Java-Io之文件File

    File是"文件"和"目录路径名"的抽象表示形式.File之间继承Object,实现了Serializable和Comparable接口,因此文件支持File对 ...

  9. MIT-6.828-JOS-lab5:File system, Spawn and Shell

    Lab 5: File system, Spawn and Shell tags: mit-6.828 os 概述 本lab将实现JOS的文件系统,只要包括如下四部分: 引入一个文件系统进程(FS进程 ...

随机推荐

  1. OPPO X9007 升级到Android5.0 Color2.1(root版) 详细纪实

    今天要做个测试,而测试APK刚好是要求最低5.0版本,正好手里有个老款手机OPPO X9007,而预装的系统是4.3,试了下虽然也能运行,但是主要功能不正常,毕竟人家APK最低要求摆在那. 反正这个手 ...

  2. NodePort,LoadBalancer还是Ingress?我该如何选择 - kubernetes

    原文:http://mp.weixin.qq.com/s/dHaiX3H421jBhnzgCCsktg 当我们使用k8s集群部署好应用的Service时,默认的Service类型是ClusterIP, ...

  3. 使用Update Strategy组件无法进行delete操作

    问题: Update Strategy组件根据字段值对目标表进行DD_DELETE操作时失效 同时,session log中报错:Target table [XXXXXXXX] does not al ...

  4. Java读取Excel的另一种方法

    除了用poi读取Excel的表格外,还可用ExcelHelper读取Excel.代码一例 String[] fieldNames = new String[] { "studentId&qu ...

  5. Condition条件变量

    条件变量是一种比较复杂的线程同步机制 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 条件变量,线程间通信提供的另一种 ...

  6. Python:黑板课爬虫闯关第四关

    第四关地址:http://www.heibanke.com/lesson/crawler_ex03/ 一开始看到的时候有点蒙,不知道啥意思,说密码需要找出来但也没说怎么找啊. 别急,随便输了个昵称和密 ...

  7. 【Vue】 ----- 浅谈vue的生命周期

    一.概念 vue生命周期,又叫生命周期钩子函数,是组件从创建到销毁的过程. 二.主要的八大生命周期 1.首先,为方便观察每个周期的特点,我们模拟一个"one"组件的创建与销毁,并在 ...

  8. .NET运行时中的监测和可观测性

    今年5月份的时候研究分布式追踪的问题知道了的拦截方式比较零散, 刚好8月份的时候看到这篇文章,这个文章总结的比较完整.存档了很久,趁今天有空翻译给大家.原文地址,校验:张蘅水 .NET是一个托管运行时 ...

  9. DSAPI 3张图片实现花开动画

    效果图 素材 代码 Dim B0, B1, B3 As Bitmap Private B As Bitmap = Nothing Private Sub Loading_Load(sender As ...

  10. .NET CAD二次开发学习 直线画矩形并转换成组

    主要代码: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...