1. 程式人生 > >study note3

study note3

blog auth 光標 應用 += time tell strip 輸出

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Deakin"
# Email: [email protected]
# Date: 2017/12/27
#第10行添加分隔符
f=open("yesterday",‘r‘,encoding="utf-8")
count=0
for line in f: #一行一行的讀,而且內存裏只保存1行,針對大文件
if count==9:
print("-------分割線--------")
print(line.strip()) #strip:去除空格和回車
count=count+1

#第10行替換為分割線
count=0
for line in f:
if count==9:
print(‘------分割線-------‘)
count +=1
continue #理解continue的意義,在count==9的時候,
print(line.strip()) #在count==9的時候,此行不執行,因此第10行的內容直接變成了分隔符,而原來的內容不見了
count +=1
PS:對比2個語句輸出不同的地方:分別為No.1的第10行插入了分隔符,No.2的第十行的句子替換為分隔符

------------------------------------------------
對文件操作的一些常用命令:
print(f.tell()) #打印光標所在位置             0
print(f.readline()) # When I was young I‘d listen to the radio
f.seek(1) #讓光標回到某位置,字節為單位
print(f.tell()) # 1
f.flush() #使更改的內容立刻寫到硬盤裏

模塊亂入:打印進度條,f.flush()的應用

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Deakin"
# Email: [email protected]
# Date: 2017/12/26

import sys,time

for i in range(10):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.1)


study note3