python学习笔记文件操作(六)

简介:

1、文件操作流程:

  1. 打开文件,得到文件句柄并赋值给一个变量

  2. 通过句柄对文件进行操作

  3. 关闭文件

如下文件:

1
2
3
4
5
6
2017 - 03 - 24  11 : 25 : 06 : 349  -  info: [debug] [AndroidBootstrap] Sending command to android: { "cmd" : "shutdown" }
2017 - 03 - 24  11 : 25 : 06 : 355  -  info: [debug] [AndroidBootstrap] Received command result  from  bootstrap
2017 - 03 - 24  11 : 25 : 06 : 356  -  info: [debug] [UiAutomator] Shutting down UiAutomator
2017 - 03 - 24  11 : 25 : 06 : 357  -  info: [debug] [UiAutomator] Moving to state  'stopping'
2017 - 03 - 24  11 : 25 : 06 : 360  -  info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data  from  client: { "cmd" : "shutdown" }
2017 - 03 - 24  11 : 25 : 06 : 361  -  info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of  type  SHUTDOWN

操作流程:

1
2
3
=  open ( 'log.txt' , 'r' )
print (f.read())
f.close()

注意: 在win系统中log文件是utf8保存的,打开文件时open函数是通过操作系统打开的文件,而win操作系统默认的是gbk编码,所以直接打开会乱码,需要f=open('hello',encoding='utf8'),hello文件如果是gbk保存的,则直接打开即可。


2、文件打开模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ========= ===============================================================
#
#     Character Meaning
#
#     --------- ---------------------------------------------------------------
#
#     '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)
#
# ========= ===============================================================


3、文件操作方法


获取文件内容

1
2
3
4
=  open ( 'log.txt' , 'r'
data  =  f.read() 
print (data)
f.close()

输出:

2017-03-24 11:25:06:349 - info: [debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}

2017-03-24 11:25:06:355 - info: [debug] [AndroidBootstrap] Received command result from bootstrap

2017-03-24 11:25:06:356 - info: [debug] [UiAutomator] Shutting down UiAutomator

2017-03-24 11:25:06:357 - info: [debug] [UiAutomator] Moving to state 'stopping'

2017-03-24 11:25:06:360 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}

2017-03-24 11:25:06:361 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN

===========================================================================================

1
2
3
4
f =  open ( 'log.txt' , 'r' )
data1 = f. read (10)  #读取10个字符。(在这里汉字也只占一个单位)
print(data1)
f.close()

输出:

2017-03-24



readline和readlines

===========================================================================================

1
2
3
=  open ( 'log.txt' , 'r' )
print (f.readlines())  #返回的是一个列表,注意换行符
f.close()

输出:

['2017-03-24 11:25:06:349 - info: [debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}\n', '2017-03-24 11:25:06:355 - info: [debug] [AndroidBootstrap] Received command result from bootstrap\n', '2017-03-24 11:25:06:356 - info: [debug] [UiAutomator] Shutting down UiAutomator\n', "2017-03-24 11:25:06:357 - info: [debug] [UiAutomator] Moving to state 'stopping'\n", '2017-03-24 11:25:06:360 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}\n', '2017-03-24 11:25:06:361 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN']

===========================================================================================

1
2
3
4
5
=  open ( 'log.txt' , 'r' )
print (f.readline())  #逐行读取
print (f.readline())
print (f.readline(),f.readline(),f.readline())  #逐行读取,每行末尾换行符
f.close()

输出:(注意每行末尾的换行符

2017-03-24 11:25:06:349 - info: [debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}


2017-03-24 11:25:06:355 - info: [debug] [AndroidBootstrap] Received command result from bootstrap


2017-03-24 11:25:06:356 - info: [debug] [UiAutomator] Shutting down UiAutomator

 2017-03-24 11:25:06:357 - info: [debug] [UiAutomator] Moving to state 'stopping'

 2017-03-24 11:25:06:360 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}

===========================================================================================

1
2
3
4
=  open ( 'log.txt' , 'r' )
for  line  in  f.readlines():
     print (line.strip())
f.close()

输出:

2017-03-24 11:25:06:349 - info: [debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}

2017-03-24 11:25:06:355 - info: [debug] [AndroidBootstrap] Received command result from bootstrap

2017-03-24 11:25:06:356 - info: [debug] [UiAutomator] Shutting down UiAutomator

2017-03-24 11:25:06:357 - info: [debug] [UiAutomator] Moving to state 'stopping'

2017-03-24 11:25:06:360 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}

2017-03-24 11:25:06:361 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN

===========================================================================================

1
2
3
4
5
=  open ( 'log.txt' , 'r' )
print (f)
for  in  f:
     print (i.strip())
f.close()

输出:

<_io.TextIOWrapper name='log.txt' mode='r' encoding='cp936'>

2017-03-24 11:25:06:349 - info: [debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}

2017-03-24 11:25:06:355 - info: [debug] [AndroidBootstrap] Received command result from bootstrap

2017-03-24 11:25:06:356 - info: [debug] [UiAutomator] Shutting down UiAutomator

2017-03-24 11:25:06:357 - info: [debug] [UiAutomator] Moving to state 'stopping'

2017-03-24 11:25:06:360 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}

2017-03-24 11:25:06:361 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN

===========================================================================================

1
2
3
4
5
6
7
8
=  0
=  open ( 'log.txt' , 'r' )
for  in  f:
     if  = =  3 :
         =  ' '.join([i.strip(),'  this  is  line  4 '])
     print (i.strip())
     + =  1
f.close()

输出:

2017-03-24 11:25:06:349 - info: [debug] [AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}

2017-03-24 11:25:06:355 - info: [debug] [AndroidBootstrap] Received command result from bootstrap

2017-03-24 11:25:06:356 - info: [debug] [UiAutomator] Shutting down UiAutomator

2017-03-24 11:25:06:357 - info: [debug] [UiAutomator] Moving to state 'stopping' this is line 4

2017-03-24 11:25:06:360 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}

2017-03-24 11:25:06:361 - info: [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN

===========================================================================================


tell和seek

1
2
3
4
5
6
=  open ( 'log.txt' , 'r' )
print (f.read( 25 ))
print (f.tell())   #取出光标所在位置
print (f.seek( 0 ))   #移动光标到指定的位置
print (f.read( 50 ))
f.close()

输出:

2017-03-24 11:25:06:349 -

25

0

2017-03-24 11:25:06:349 - info: [debug] [AndroidBo

注意:read后不管是中文字符还是英文字符,都统一算一个单位,read(6),此刻就读了6个中文字符;而seek和tell对于英文字符就是占一个,中文字符占三个,区分与read()的不同.

===========================================================================================


flush:同步把数据从缓存移动到磁盘上去

1
2
3
4
5
6
7
8
9
10
11
12
#进度条实例
import  sys,time
for  in  range ( 30 ):
     sys.stdout.write( "*" )
     sys.stdout.flush()
     time.sleep( 0.1 )
 
#print的flush
import  sys,time
for  in  range ( 30 ):
     print ( '*' ,end = '',flush = True )
     time.sleep( 0.1 )

===========================================================================================

其他扩展:

#truncate():截断数据(不能在r模式下)
#在w模式下:先清空,再写,再截断
#在a模式下:直接将指定位置后的内容截断
# r+:光标默认在0位置开始写,从0开始覆盖数据
# w+:先清空,再写读
# a+:光标默认在最后位置

===========================================================================================

with:

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

1
2
with  open ( 'log.txt' , 'r' ) as f:
     print (f.readline())

在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

1
2
3
4
#with 同时管理多个文件对象
with  open ( 'log1' , 'r' ) as f_read,  open ( 'log2' , 'w' ) as f_write:
     for  line  in  f_read:
         f_write.write(line)
















本文转自cqtesting51CTO博客,原文链接:http://blog.51cto.com/cqtesting/1959698  ,如需转载请自行联系原作者

相关文章
|
5月前
|
存储 Python
Python文件操作(1)
【10月更文挑战第17天】
141 60
Python文件操作(1)
|
5月前
|
数据采集 存储 Python
Python文件操作2
【10月更文挑战第18天】
34 2
Python文件操作2
|
5月前
|
网络协议 Java Linux
PyAV学习笔记(一):PyAV简介、安装、基础操作、python获取RTSP(海康)的各种时间戳(rtp、dts、pts)
本文介绍了PyAV库,它是FFmpeg的Python绑定,提供了底层库的全部功能和控制。文章详细讲解了PyAV的安装过程,包括在Windows、Linux和ARM平台上的安装步骤,以及安装中可能遇到的错误和解决方法。此外,还解释了时间戳的概念,包括RTP、NTP、PTS和DTS,并提供了Python代码示例,展示如何获取RTSP流中的各种时间戳。最后,文章还提供了一些附录,包括Python通过NTP同步获取时间的方法和使用PyAV访问网络视频流的技巧。
1034 4
PyAV学习笔记(一):PyAV简介、安装、基础操作、python获取RTSP(海康)的各种时间戳(rtp、dts、pts)
|
5月前
|
Python
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
使用Python的socket库实现客户端到服务器端的图片传输,包括客户端和服务器端的代码实现,以及传输结果的展示。
239 3
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
|
5月前
|
JSON 数据格式 Python
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
本文介绍了如何使用Python的socket模块实现客户端到服务器端的文件传输,包括客户端发送文件信息和内容,服务器端接收并保存文件的完整过程。
292 1
Socket学习笔记(一):python通过socket实现客户端到服务器端的文件传输
|
5月前
|
关系型数据库 MySQL 数据库
Mysql学习笔记(四):Python与Mysql交互--实现增删改查
如何使用Python与MySQL数据库进行交互,实现增删改查等基本操作的教程。
105 1
|
5月前
|
Ubuntu Linux Python
Ubuntu学习笔记(六):ubuntu切换Anaconda和系统自带Python
本文介绍了在Ubuntu系统中切换Anaconda和系统自带Python的方法。方法1涉及编辑~/.bashrc和/etc/profile文件,更新Anaconda的路径。方法2提供了详细的步骤指导,帮助用户在Anaconda和系统自带Python之间进行切换。
258 1
|
5月前
|
索引 Python
Python学习笔记编程小哥令狐~持续更新、、、(上)
Python学习笔记编程小哥令狐~持续更新、、、(上)
73 2
|
5月前
|
存储 Python
Python学习笔记编程小哥令狐~持续更新、、、 (下)
Python学习笔记编程小哥令狐~持续更新、、、 (下)
57 1
|
5月前
|
Java 编译器 Go
Python学习笔记--- day01计算机基础和环境搭建(一)
Python学习笔记--- day01计算机基础和环境搭建(一)
72 2