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

  • 时间:
  • 浏览:0

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

5

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

6

<_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"}

16

tell和seek

1

23

9

print(f.readline())

输出:

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

f.close()

5

import sys,time

19

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

1

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

获取文件内容

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

for in f:

print(data1)

4

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

11

14

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

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

通过句柄对文件进行操作

        = ''.join([i.strip(),' this is line 4'])



readline和readlines

10

f.close()

4

with open('log1','r') as f_read, open('log2','w') as f_write:

f.close()

5

3

2

f.close()

4

['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:31000 - 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']

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

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

f.close()

    += 1

6

data1 = f.read(10) 

with:

2

for line in f.readlines():

print(data)

2

    print('*',end='',flush=True)

= open('log.txt','r')

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

print(f.read(1000))

打开文件,得到文件句柄并赋值给另3个变量

4

    print(f.readline())

1

2

1

print(f.read())

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

    for line in f_read:

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

2

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

print(f.readline()) 

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

15

输出:

为了除理打开文件后忘记关闭,都必须通过管理上下文,即:

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

print(f.readlines()) 

3、文件操作方式

print(f.seek(0))  

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

21

操作流程:

1

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

8

1

1

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

22

5

1

f.close()

2

输出:

= 0

f.close()

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

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

3

12

import sys,time

= open('log.txt','r')

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

4

= open('log.txt','r')

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

= open('log.txt','r')

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

3

1

17

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

输出:

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

3

        f_write.write(line)

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

4

for in f:

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

2

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

4

25

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

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

输出:

= open('log.txt','r')

data = f.read() 

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

8

for in range(1000):

2

f = open('log.txt','r')

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

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

    sys.stdout.flush()

2017-03-24

f.close()

= open('log.txt','r')

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

10

1

12

20

1

2、文件打开模式

18

print(f.read(25))

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

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

3

3

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

= open('log.txt','r')

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

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

3

f.close()

7

8

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

with open('log.txt','r') as f:

1

2

4

print(f.readline(),f.readline(),f.readline()) 

2

7

2

print(f.tell())  

4

11

    print(i.strip())

6

关闭文件

1、文件操作流程:

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

3

2

2

    time.sleep(0.1)

6

2

4

3

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

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

1

3

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

    time.sleep(0.1)

5

5

6

for in range(1000):

7

9

1

print(f)

一点扩展:

= open('log.txt','r'

5

13

    if == 3:

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

3

4

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

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

如下文件:

3

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

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

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

3

    print(line.strip())

    sys.stdout.write("*")

输出:

    print(i.strip())