本篇博客主要为 频道练习题模块补充题目,暂定每天提供 5 or 6 道测试题,后面可能会更多哦~。

本篇博客对【进阶语法】→ 【文件】 进行出题。

以下题目,默认将正确答案,放置在选项 A 位置


文章目录

  • 知识点:python 进阶语法-文件
  • 第 1 题:
  • 第 2 题:
  • 第 3 题:
  • 第 4 题:
  • 第 5 题:
  • 试题仓库地址如下:


知识点:python 进阶语法-文件

第 1 题:

题目难度:1 星
题干(问题描述):
编写代码,逐行读取诗歌,文件内容如下:

山行
远上寒山石径斜,
白云生处有人家。
停车坐爱枫林晚,
霜叶红于二月花。

选项 A:

try:
    with open('shi.txt', "r", encoding="utf-8") as f:
        for line in f.readlines():
            print(line.strip())
except Exception as ex:
    print(ex)

选项 B:

try:
    with open('shi.txt', "r", encoding="utf-8") as f:
        for line in f.readline():
            print(line.strip())
except Exception as ex:
    print(ex)

选项 C:

try:
    with open('shi.txt', "r", encoding="utf-8") as f:
        for line in f.read():
            print(line.strip())
except Exception as ex:
    print(ex)

选项 D:

try:
    with open('shi.txt', "r", encoding="utf-8") as f:
        for line in f.readlines():
            print(line.read())
except Exception as ex:
    print(ex)

正确答案:A

第 2 题:

题目难度:1 星

下述代码,哪个选项可以正确拷贝图片 A.pngA-copy.png,测试图片为:

Python代码阅读程序_python 进阶语法

选项 A:

try:
    with open('A.png', "rb") as fr:
        with open("A-copy.png","wb") as fw:
            fw.write(fr.read())
except Exception as ex:
    print(ex)

选项 B:

try:
    with open('A.png', "wb") as fr:
        with open("A-copy.png","rb") as fw:
            fw.write(fr.read())
except Exception as ex:
    print(ex)

选项 C:

try:
    with open('A.png', "w") as fr:
        with open("A-copy.png","r") as fw:
            fw.write(fr.read())
except Exception as ex:
    print(ex)

选项 D:

try:
    with open('A.png', "a") as fr:
        with open("A-copy.png","r") as fw:
            fw.write(fr.read())
except Exception as ex:
    print(ex)

正确答案:A

第 3 题:

题目难度:2 星
题干(问题描述):
编写代码,检测电脑某文件夹中的所有后缀名是 json 的文件。

选项 A:

import os


def find_txtfile():
    for p1 in os.listdir(path):
        if p1.endswith(".txt"):
            print(p1)


if __name__ == '__main__':
    path = "E:\c_test"  # 测试路径
    find_txtfile()

选项 B:

import os


def find_txtfile():
    for p1 in os.listdir(path):
        if ".txt" in p1:
            print(p1)


if __name__ == '__main__':
    path = "E:\c_test"  # 测试路径
    find_txtfile()

选项 C:

import os


def find_txtfile():
    for p1 in os.listdir(path):
    	print(p1)


if __name__ == '__main__':
    path = "E:\c_test"  # 测试路径
    find_txtfile()

选项 D:

import os


def find_txtfile():
    for p1 in os.listdir(path):
        if os.path.join(path, p1):
            print(p1)


if __name__ == '__main__':
    path = "E:\c_test"  # 测试目录
    find_txtfile()

正确答案:A

第 4 题:

题目难度:1 星
题干(问题描述):
编写代码,将用户输入内容追加保存到 user_input.txt 文件中,每次输入的数据单独存储一行。

选项 A:

user_input_file = open("user_input.txt", "a+", encoding="utf-8")

while True:
    user_str = input("请输入你要存储的内容:")
    if user_str == "exit":
        print("用户退出!")
        break
    else:
        user_input_file.write(f"{user_str}\n")

user_input_file.close()

选项 B:

user_input_file = open("user_input.txt", "a+", encoding="utf-8")

while True:
    user_str = input("请输入你要存储的内容:")
    if user_str == "exit":
        print("用户退出!")
        break
    else:
        user_input_file.write(f"{user_str}")

user_input_file.close()

选项 C:

user_input_file = open("user_input.txt", "r", encoding="utf-8")

while True:
    user_str = input("请输入你要存储的内容:")
    if user_str == "exit":
        print("用户退出!")
        break
    else:
        user_input_file.write(f"{user_str}")

user_input_file.close()

选项 D:

user_input_file = open("user_input.txt", "wb", encoding="utf-8")

while True:
    user_str = input("请输入你要存储的内容:")
    if user_str == "exit":
        print("用户退出!")
    else:
        user_input_file.write(f"{user_str}")

user_input_file.close()

正确答案:A

第 5 题:

题目难度:2 星
题干(问题描述):
编写代码,输出用户指定目录所有文件与文件大小。

选项 A:

import os


def find_txtfile():
    for p1 in os.listdir(path):
        file_path = os.path.join(path, p1)

        if os.path.isfile(file_path):
            file_size = os.path.getsize(file_path)
            print("文件名为:", p1, "文件大小是:", file_size)


if __name__ == '__main__':
    path = "E:\c_test"  # 测试路径
    find_txtfile()

选项 B:

import os


def find_txtfile():
    for p1 in os.listdir(path):


        if os.path.isfile(path):
            file_size = os.path.getsize(path)
            print("文件名为:", p1, "文件大小是:", file_size)


if __name__ == '__main__':
    path = "E:\c_test"  # 测试目录
    find_txtfile()

选项 C:

import os


def find_txtfile():
    for p1 in os.listdir(path):
        file_path = os.path.join(path, p1)

        if os.path.isfile(file_path):
            file_size = os.getsize(file_path)
            print("文件名为:", p1, "文件大小是:", file_size)


if __name__ == '__main__':
    path = "E:\c_test"  # 测试路径
    find_txtfile()

选项 D:

import os


def find_txtfile():
    for p1 in os.listdir(path):
        file_path = os.path.join(path, p1)

        if os.path.isdir(file_path):
            file_size = os.getsize(file_path)
            print("文件名为:", p1, "文件大小是:", file_size)


if __name__ == '__main__':
    path = "E:\c_test"  # 测试路径
    find_txtfile()

正确答案:A