day08

1.定义一个函数,函数创建一个文件并写入“hello world” , 定义另外一个函数, 函数读取文件“hello world” 并打印出来

def hello_write():
    f = open("1.txt","w")
    f.write("hello world")
    f.close()

def hello_read():
    f = open("1.txt","r")
    content = f.read()
    print(content)

hello_write()
hello_read()

2.根据第一题改编代码,定义一个函数向文件“2.txt”中写入99乘法表,定义另一个函数从“2.txt”中读取99乘法表并打印出来​

def write_99():
      # 向文件中写入99乘法表
    # 以“w”方式打开文件
    f = open("2.txt", 'w')
    # 两层循环构造99乘法表
    for i in range(1, 10):
        for j in range(1, i + 1):
              # 将每一个乘式写入到文件中
            f.write("%d * %d = %-2d	" % (j, i, j * i))  # 	为制表符,即为一个tab键
        # 写完一行,换行,写入换行符
        f.write("
")
    f.close()

def read_99():
    f = open("2.txt", 'r')
    content = f.read()
    f.close()
    print(content)

write_99()
read_99()

3.定义一个函数,用户输入年龄,性别,家庭住址,通过函数把信息存到文件中,并显示“保存成功”

def cun_chu():
    # 创建一个用来保存信息的字典
    a = {}
    a['name'] = input("name:")
    a['age'] = input("age:")
    a['gender'] = input("gender:")
    a['addr'] = input("addr:")
    f = open("3.txt","w")
    # 将字典转化为字符串写入到文件中,文件中不能写入字典(python对象)
    f.write(str(a))
    f.close()
    print("保存成功")
cun_chu()

​4.定义两个函数:write_str:在文件“4.txt”中写入100遍“键盘敲烂,月薪过万”并换行,copy_file函数:将“4.txt”传入copy_file函数当做参数,对“4.txt”文件进行备份,备份文件名为“4-复件.txt”

def write_str():
    f = open("4.txt","w",encoding="utf-8")
    for i in range(100):
        f.write("键盘敲烂,月薪过万
")
    f.close()

def copy_file(file):
    new_file_name = file[:-4]+"-复件"+file[-4:]
    read_file = open(file,"r",encoding="utf-8")
    write_file = open(new_file_name,"w",encoding="utf-8")
    while True:
        content = read_file.read(1)
        if content:
            write_file.write(content)
        else:
            break
    read_file.close()
    write_file.close()

write_str()
copy_file("4.txt")

​5.在当前目录下创建5文件夹,将当前目录下所有文件复制到5文件夹内(将文件名后加“-复件”,如1.txt--》1-复件.txt),并打印出5文件夹内的文件列表

import os
# 获取当前所在路径文件列表
file_list = os.listdir()
# 创建“5”文件夹
os.mkdir("5")
# 循环将每一个文件复制到“5”文件夹中
for file in file_list:
    # “.idea”文件为隐藏文件,忽略此文件
    if file == ".idea":
        continue
    # 打开此文件
    read_file = open(file,"r",encoding="utf-8")
    # 获取文件内容
    file_content = read_file.read()
    # 关闭文件
    read_file.close()
    # 进入到“5”文件夹中
    os.chdir("5")
    # 获取文件名中“.”的位置,因为不是所有的文件后缀都是“.txt”,还有“.py”,还有多个“.”的情况,因此需要从右侧开始找“.”的位置
    dot_index = file.rfind(".")
    # 组件新的文件名
    new_file_name =file[:dot_index]+"-复件"+file[dot_index:]
    # 打开文件并写入内容
    write_file = open(new_file_name,"w",encoding="utf-8")
    write_file.write(file_content)
    write_file.close()
    # “../”代表上一级目录
    os.chdir("../")
# 进入到“5”文件夹,获取文件列表并打印
os.chdir("5")
new_file_list = os.listdir()
print(new_file_list)

补充:

1.a,b=b,a交换两个变量的过程(昨天反馈较差的点:知道使用组包和拆包可以交换两个变量的值,不占用大家时间统一讲解,在这里简单解释一下)

a = 1
b = 2
c = b,a
print(c) # (2,1)--类型为元组
# 再参照函数返回多个值:
def get_tuple():
    return 1,2,3
g = get_tuple()  # g此时为元组
d,e,f = get_tuple()  # 此时d=1,e=2,f=3
# 同理:
a,b = b,a
即a,b = (b,a)
即a,b = (2,1)
a = 2
b = 1

2.zip函数相关问题

a = [1,2,3]
b = [4,5,6]
c = zip(a,b)
print(list(c))  # [(1,4),(2,5),(3,6)]
print(list(c))  # 结果为空

此现象因为zip函数返回的是个迭代器(后面会讲到,现在不为大家作过多解释) ,list函数对其操作时对其进行了遍历操作

a = [1,2,3]
b = [4,5,6]
c = zip(a,b)
next(c)  # (1,4)
next(c)  # (2,5)
next(c)  # (3,6) 
next(c)  # 报错
# 类似于今天文件操作的光标,取一次向后移动一位,list函数对其操作的时候类似于读取文件,将“光标”移动到了最后,再次list操作就为空了,当前有这个概念即可,后面会系统的讲解这一块
原文地址:https://www.cnblogs.com/kangwenju/p/12854319.html