python 进阶语法-文件 5 道练习题 | Python技能树征题

本文提供5道关于Python进阶文件操作的练习题,涉及文件读写、图片复制、检测json文件、用户输入追加及目录文件信息显示。通过实践巩固文件操作技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

知识点: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,测试图片为:
在这里插入图片描述

选项 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

试题仓库地址如下:

https://codechina.youkuaiyun.com/hihell/question

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦想橡皮擦

如有帮助,来瓶可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值