ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Python第二阶段学习 day03

2021-07-01 09:04:27  阅读:149  来源: 互联网

标签:文件 txt Python data day03 file print 第二阶段 open


前情回顾

1. 基本命令

chmod  df  date  shutdown  ln

2. vi 编辑器

   i  o   esc  :wq   :q!

3. 创建用户

   useradd -> passwd -> vi /etc/sudoers -> vi /etc/passwd

4. 软件安装

   sudo apt update
   sudo apt install
   sudo apt remove

5. ssh 远程链接

   ssh  tarena@172.40.45.25
   scp  file.txt  tarena@172.40.45.25:/home/..

   ssh-keygen-> 公钥  私钥
   服务器记录公钥 : ~/.ssh/authorized_keys

Linux操作系统环境使用

理论知识:
   1. 操作系统的作用
   2. Linux的操作系统特点和应用场景

掌握内容:
   1. Linux 文件结构特点
   2. Linux操作系统下文件位置的表达

实操内容:
   1. Linux基础命令使用 (重点)
   2. 常用Linux功能
      压缩包处理  软件安装  用户创建   vi使用
   3. 远程管理服务器
   4. 服务端启动Python程序

熟练在Linux系统下进行开发工作和环境部署

《鸟哥私房菜 基础》


练习01:
使用dict.txt 完成

编写一个函数,参数传入一个单词,返回这个单词
的解释,如果单词不存在则返回 "Not Found"

注意: 每个单词一行
      单词和解释之间有空格
      单词按照从小到大排列


练习02:
编写一个函数,函数参数传入一个指定文件,执行
函数后,将该文件复制一份到当前程序运行目录下

注意:
    指定文件: 可能是文本文件也可能是二进制文件
    plus : 假设文件比较大,不要一次性读取

提示: 复制文件本质是从一个文件读取写入另一个文件

def copy(filename):
    pass


练习03
编写一个程序,循环的向文件 my.log 中写入
内容: (每次写一行,每次写入间隔2秒)
1. Tue Jun  1 17:25:47 2021
2. Tue Jun  1 17:25:49 2021
3. Tue Jun  1 17:25:51 2021
4. Tue Jun  1 17:29:18 2021
5. Tue Jun  1 17:29:20 2021

要求: 每行要实时的显示出来
      当程序终止后,如果重新启动继续往下写
      序号能够衔接
提示 :import time -> sleep(2)   ctime()


作业 : 1. 先熟悉今天的函数 open read write

有一个列表,里面每一项都是一个文本文件位置
如: ["../day01/1.txt","../day02/2.txt"..]

请编写一个程序,将列表中的文件合并为1个大文件
命名为 union.txt


"""
使用dict.txt 完成

编写一个函数,参数传入一个单词,返回这个单词
的解释,如果单词不存在则返回 "Not Found"

注意: 每个单词一行
      单词和解释之间有空格
      单词按照从小到大排列
"""

def query_word(word):
    file = open("dict.txt") # r 打开
    # 每次取一行进行比对
    for line in file:
        tmp = line.split(' ',1) # tmp->[word,xxx]
        if tmp[0] > word:
            break # 如果遍历的单词已经大于Word 就没必要找了
        elif tmp[0] == word:
            return tmp[1].strip() # 去除字符串两侧的空格
    return "Not Found"

print(query_word("abc"))

"""
编写一个函数,函数参数传入一个指定文件,执行
函数后,将该文件复制一份到当前程序运行目录下

注意:
    指定文件: 可能是文本文件也可能是二进制文件
    plus : 假设文件比较大,不要一次性读取

提示: 复制文件本质是从一个文件读取写入另一个文件
"""
def copy(filename):
    fr = open(filename,'rb') # 源文件
    new = filename.split('/')[-1] # 取文件名
    fw = open(new,'wb')
    # 边读边写
    while True:
        data = fr.read(1024)
        if not data:
            break  # 到了文件结尾
        fw.write(data)
    fr.close()
    fw.close()

copy("/home/tarena/下载/gyy.png")







"""
练习03
编写一个程序,循环的向文件 my.log 中写入
内容: (每次写一行,每次写入间隔2秒)
1. Tue Jun  1 17:25:47 2021
2. Tue Jun  1 17:25:49 2021
3. Tue Jun  1 17:25:51 2021
4. Tue Jun  1 17:29:18 2021
5. Tue Jun  1 17:29:20 2021

要求: 每行要实时的显示出来
      当程序终止后,如果重新启动继续往下写
      序号能够衔接
提示 :import time -> sleep(2)   ctime()
"""
import time

file = open("my.log",'a+',buffering=1)

file.seek(0,0) # 文件偏移量到开头
# 序号  初始 = 行数 + 1
n = len(file.readlines()) + 1

while True:
    msg = "%d. %s\n"%(n,time.ctime())
    file.write(msg)
    n += 1 # 序号增加
    time.sleep(2) # 间隔2s








"""
文件读写缓冲
"""
# 设置行缓冲
# file = open("file.txt",'w',buffering=1)

# 设置缓冲大小
file = open("file.txt",'wb',buffering=10)

while True:
    msg = input(">>")
    if not msg:
        break
    file.write(msg.encode())
    # file.flush() # 刷新缓冲

file.close()
"""
字节串类型

是否所有字符串都能转换为字节串  是
是否所有字节串都能转换为字符串  不是
"""
# 纯英文字符字节串
bytes1 = b"Hello world"
print(type(bytes1))

# 非英文字符 使用encode()转换
bytes2 = "你好".encode()
print(bytes2)

# decode()  字节串--》字符串
print(bytes2.decode())
print(b'\xe4\xbd\xa1\xf3\xa3\xbd'.decode())
"""
打开文件 演示
"""

# 读方式打开文件
# file = open("../day02/2.txt",'r')

# 写打开
# file = open("file.txt",'w') # 自动创建/清空内容

# 追加方式
file = open("file.txt","a")

# 操作文件

# 关闭
file.close()




"""
文件读取操作 演示

* 如果读取到了文件结尾继续读会返回空字串
"""
# 读方式打开文件
# file = open("file.txt",'r')

# 二进制读
file = open("file.txt",'rb')

# 读取文件内容
data = file.read()
print(data.decode())

# 每次读取一个字符,将整个文件读完并按照原格式打印
# while True:
#     data = file.read(1)
#     if data == "":
#         break
#     print(data,end="")

# 按行读取
# line = file.readline()
# print(line)

# 读取多行内容
# lines = file.readlines(16)
# print(lines) # 列表 每项是一行内容

# 迭代获取每行
# for line in file:
#     print(line)

file.close()
"""
文件偏移量
"""

# 打开文件 可读可写
file = open("file.txt","wb+")

file.write(b"Hello Kitty\n")
file.flush()
print("文件偏移量:",file.tell())

# 操作文件偏移量  0 开头  1 当前  2 结尾
file.seek(-3,2)

data = file.read()
print(data)

file.close()
"""
with 语句块简单示例
"""
with open("file.txt",'r') as file:
    data = file.read()
    print(data)

# 语句块结束 file也会销毁
"""
文件写操作演示
"""
# 写方式打开文件
file = open("file.txt",'wb')

# file = open("file.txt",'a') # 追加打开

# 写入内容
file.write("hello,死鬼\n".encode())
n = file.write("哎呀,干啥\n".encode())
print(n)

# data = [
#     "六一快乐\n",
#     "今日躺平\n"
# ]
# # 将列表中的每一项分别写入
# file.writelines(data)

file.close()

在这里插入图片描述
在这里插入图片描述

标签:文件,txt,Python,data,day03,file,print,第二阶段,open
来源: https://blog.csdn.net/m0_55389447/article/details/118378654

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有