文件内光标的移动 函数基础 定义函数的三种形式

# with open(r'a.txt', 'r', encoding='utf-8')as f:
# data1=f.read()
# print('>1>:',data1)
# print(f.tell()) # 44 只有一种情况下,光标的意思是字符
# data2=f.read()
# print('>2>:',data2) # 第一次有结果,第二次没有,第一次读取数据后光标已经移到了文件尾

# 只有一种情况下,光标以字符为单位:文件以rt方式打开,read()
# with open(r'a.txt', 'rt', encoding='utf-8')as f:
# print(f.read(8))
# print(f.tell())
# f.seek(0,0) # seek 字节为单位
# print(f.read(8))
# print(f.tell())
# f.seek(13, 0)
# print(f.read(3))
# print(f.tell())

# 只有0模式能够在t模式下使用
# with open(r'a.txt', 'rt', encoding='utf-8')as f:
# print(f.read(3))
# f.seek(3,3)
# print(f.read(3))

# with open(r'a.txt', 'rb')as f:
# print(f.read(6)) # b'hello\xe4'
# f.seek(2,1)
# print(f.tell())
# print(f.read().decode('utf_8'))

# with open(r'a.txt', 'rb')as f:
# f.seek(-3,2) # 光标倒着移动3个
# print(f.tell())
# f.seek(0,2) # 直接把光标移到末尾

# tail -f access.log # 打开文件,读追加的内容

# import time
# with open(r'b.txt','rb')as f:
# f.seek(0,2) # 光标移动到末尾
# while True:
# line=f.readline() # 所有的数据类型自带布尔值,一旦为空就是False
# if line:
# print(line,end='')
# else:
# time.sleep(0.05)

# with open(r'b.txt', 'a', encoding='utf-8')as f:
# f.write('you are my apple\n')
# f.flush() # 追加写并立即保存到硬盘

# import time
# with open(r'b.txt','rb')as f:
# f.seek(0,2)
# while True:
# line=f.readline()
# if line:
# print(line) #‘end=''’bytes类型中没有换行这个意思,end=''就是普通的字符串
# else:
# time.sleep(0.05)

# import time
# with open(r'b.txt','rb')as f:
# f.seek(0,2)
# while True:
# line=f.readline()
# if line:
# print(line.decode(),end='')
# else:
# time.sleep(0.05)

# 文件截断
# with open(r'a.txt', 'a', encoding='utf-8')as f:
# f.truncate(3) # 只保留前三个字节

# 为什么要有函数?没有函数带来的困扰
# 组织结构不清晰,可读性差
# 代码冗余
# 可扩展性差
# 什么是函数?
# 具备某一个功能的工具
# 先定义后调用
# 函数的分类
# 内置函数:len(),max(10,100),
# 自定义函数:def
# 自定义函数:
# def 函数(参数1,参数2,...): # 不写参数可以,但是()要有
# (缩进)'''注释''' # help(函数名) 注释必须要有,但是还是可选的
# 函数体 # 必须有
# return # 返回值 可选
# ===============
# You are my apple
# ===============

# def print_tag():
# print('===============')
# def print_msg():
# print('You are my apple')

# print(print_tag) # <function print_tag at 0x00000178BEC54DC8> 内存地址
# 相当于print_tag=<function print_tag at 0x00000178BEC54DC8>

# print_tag()
# print_msg()
# print_tag()
# '''
# ===============
# You are my apple
# ===============
# '''
# def print_tag():
# print('*'*15)
# def print_msg():
# print('You are my apple')
# print_tag()
# print_msg()
# print_tag()
# '''
# ***************
# You are my apple
# ***************
# ''' # 扩展性高了

# def auth():
# name=input('name>>:').strip()
# password=input('password>>:').strip()
# if name == 'OBOS' and password == '123':
# print('登陆成功')
# else:
# print('用户名或者密码错误')
#
# auth()

# 定义函数阶段都发生了什么事?
# 定义函数阶段只检测语法,不执行代码。语法没问题就定义成功了,不会报错。

# 先调用后使用
# def foo():
# print('from foo')
# bar()
# def bar():
# print('from bar')

# 定义函数的三种形式
# 第一种:无参函数 #
# def foo():
# def bar():
# def print_tag():
# def print_msg():
# x=10
# y=11
# if x >= y:
# print('x')
# else:
# print('y')
# 第二种:有参函数
# def my_max(x,y):
# if x >= y:
# print(x)
# else:
# print(y)
# my_max(1,2)
# def auth(name,password):
# if name == 'OBOS' and password == '123':
# print('登陆成功')
# else:
# print('用户名或者密码错误')
#
# def love():
# name=input('name>>:').strip()
# password=input('password>>:').strip()
# auth(name,password)
# love()
# 函数尽可能各做各的,把一个个功能单独提取出来
# 第三种:空函数
# def auth():
# '''
# '''
# pass
# def put():
# pass
# def get():
# pass
# def is():
# pass
# 体现程序的组织结构

猜你喜欢

转载自www.cnblogs.com/0B0S/p/11952840.html