资讯专栏INFORMATION COLUMN

仅需10道题轻松掌握Python文件处理 | Python技能树征题

caoym / 3063人阅读

摘要:问题描述已知存在二进制文件,如何正确向此文件追加写入文本数据,请从以下选项中选出你认为正确的答案正确答案第题压缩文件的读写知识点描述读写或格式的压缩文件。

0.前言

所有应用程序都需要处理输入和输出,文件是用于获取输入和保存输出的常用载体,文件可以是文本文档、图片、程序等等,我们就通过 10Python 编程题来掌握解决常见文件处理问题的方法吧!

1. 第 1 题:文件路径名的处理

知识点描述:使用路径名来获取文件名,目录名,绝对路径。
问题描述:有一文件路径如下:“/home/brainiac/Documents/csdn/hello_world.py”,请从以下选项中选出可以获取文件名 “hello_world.py” 的选项:
A.

import ospath = "/home/brainiac/Documents/csdn/hello_world.py"file_name = os.path.dirname(path)print(file_name)

B.

import ospath = "/home/brainiac/Documents/csdn/hello_world.py"file_name = os.path.abspath(path)print(file_name)

C.

import ospath = "/home/brainiac/Documents/csdn/hello_world.py"file_name = os.path.basename(path)print(file_name)

D.

import ospath = "/home/brainiac/Documents/csdn/hello_world.py"file_name = os.path.splitext(path)[-1]print(file_name)

正确答案: C

2. 第 2 题:检测文件是否存在

知识点描述:检测指定文件或目录是否存在。
问题描述:请从以下选项中选出可以检测 “/etc/passwd” 文件是否存在的选项:
A.

import osexist = os.path.exists("etc/passwd")print(exist)

B.

import osexist = os.path.isdir("etc/passwd")print(exist)

C.

import osexist = os.path.isdir("/etc/passwd")print(exist)

D.

import osexist = os.path.isfile("/etc/passwd")print(exist)

正确答案: D

3. 第 3 题:获取指定文件夹下的文件列表

知识点描述:获取文件系统中指定目录下的所有文件列表。
问题描述:获取 “/etc” 目录中所有 python 文件(以 “.py” 作为文件后缀)列表,请从以下选项中选出你认为正确的选项:
A.

import ospath = "/usr/lib/python3/dist-packages"names = [name for name in os.listdir(path)]print(names)

B.

import ospath = "/usr/lib/python3/dist-packages"names = [name for name in os.listdir(path) if name.endswith(".py")]print(names)

C.

import ospath = "/usr/lib/python3/dist-packages"names = [name for name in os.listdir(path) if os.path.isfile(name) and name.endswith(".py")]print(names)

D.

import ospath = "/usr/lib/python3/dist-packages"names = [name for name in os.listdir(path) if name.endswith("*.py")]print(names)

正确答案: B

4. 第 4 题:文本文件的读写

知识点描述:读写使用不同编码方式的文本文件。
问题描述:假设存在一文件 “text_1.txt”,如何向其中再添加两行新数据,请从以下选项中选出你认为正确的选项:
A.

new_line_1 = "New line 1"new_line_2 = "New line 2"with open("text_1.txt", "rt") as f:    f.write(new_line_1+"/n")    f.write(new_line_2+"/n")

B.

new_line_1 = "New line 1"new_line_2 = "New line 2"with open("text_1.txt", "at") as f:    f.write(new_line_1)    f.write(new_line_2)

C.

new_line_1 = "New line 1"new_line_2 = "New line 2"with open("text_1.txt", "wt") as f:    f.write(new_line_1+"/n")    f.write(new_line_2+"/n")

D.

new_line_1 = "New line 1"new_line_2 = "New line 2"with open("text_1.txt", "at") as f:    f.write(new_line_1+"/n")    f.write(new_line_2+"/n")

正确答案: D

5. 第 5 题:将打印输出到文件中

知识点描述:将 print() 函数的输出重定向到指定日志文件中。
问题描述:将当前时间写入日志文件 “log.txt” 中,并记录函数执行结果,请从以下选项中选出你认为正确的答案:
A.

from datetime import datetimedef hello_world(num):    return "Hello world {}!".format(num)for i in range(10):    with open("log.txt", "at") as f:        print(str(datetime.today()) + "/t" + hello_world(i), file=f)

B.

from datetime import datetimedef hello_world(num):    return "Hello world {}!".format(num)for i in range(10):    with open("log.txt", "at") as f:        print(datetime.today() + "/t" + hello_world(i), file=f)

C.

from datetime import datetimedef hello_world(num):    return "Hello world {}!".format(num)for i in range(10):    with open("log.txt", "wt") as f:        print(datetime.today() + "/t" + hello_world(i))

D.

from datetime import datetimedef hello_world(num):    return "Hello world {}!".format(num)for i in range(10):    with open("log.txt", "wt") as f:        print(str(datetime.today()) + "/t" + hello_world(i), file=f)

正确答案: A

6. 第 6 题:二进制文件的读写

知识点描述:读写二进制文件,如图片、声音文件等。
问题描述:已知存在二进制文件 “test.bin”,如何正确向此文件追加写入文本数据,请从以下选项中选出你认为正确的答案:
A.

with open("test.bin", "at") as f:    text = "Hello World!/n"    f.write(text)

B.

with open("test.bin", "wb") as f:    text = "Hello World!/n"    f.write(text.encode("utf-8"))

C.

with open("test.bin", "ab") as f:    text = "Hello World!/n"    f.write(text.encode("utf-8"))

D.

with open("test.bin", "ab") as f:    text = "Hello World!/n"    f.write(text)

正确答案: C

7. 第 7 题:压缩文件的读写

知识点描述:读写 gzip 或 bz2 格式的压缩文件。
问题描述:请从以下选项中选择能够将文本文件 “text.txt” 内容写入压缩文件 “compress.gz” 的程序,且要求压缩程度最佳:
A.

import gziptext = "text.txt"with gzip.open("compress.gz", "wt", compresslevel = 9) as f:    f.write(text)

B.

import gziptext = "text.txt"with gzip.open("compress.gz", "wt", compresslevel = 0) as f:    f.write(text)

C.

import gziptext = "text.txt"with open(text, "rt") as file:    read_text = file.read()    with gzip.open("compress.gz", "wt", compresslevel = 9) as f:        f.write(read_text)

D.

import gziptext = "text.txt"with open(text, "rt") as file:    read_text = file.read()    with gzip.open("compress.gz", "wt", compresslevel = 0) as f:        f.write(read_text)

正确答案:C

8. 第 8 题:以固定数据块大小读取文件

知识点描述:以固定长度数据块长度迭代读取文件,而非逐行读取。
问题描述:存在一文件 “test.bin”,编写程序每次读取数据块大小为 16B,直到文件末尾:
A.

from functools import partialRECORD_SIZE = 16with open("test.bin", "rt") as f:    records = iter(partial(f.read, RECORD_SIZE), b"")    for r in records:        print(r)

B.

from functools import partialRECORD_SIZE = 16with open("test.bin", "rb") as f:    records = iter(partial(f.read, RECORD_SIZE), b"")    for r in records:        print(r)

C.

from functools import partialRECORD_SIZE = 16 * 8with open("test.bin", "rt") as f:    records = iter(partial(f.read, RECORD_SIZE), b"")    for r in records:        print(r)

D.

from functools import partialRECORD_SIZE = 16with open("test.bin", "rt") as f:    records = iter(partial(f.read, RECORD_SIZE), "")    for r in records:        print(r)

正确答案:B

9. 第 9 题:增加或改变已打开文件的编码方式

知识点描述:在不关闭已打开文件前提下改变文件的编码方式。
问题描述:如何为一个以二进制模式打开的文件添加 “utf-8” 编码方式,请从以下选项中选出你认为正确的答案:
A.

import urllib.requestimport iowith urllib.request.urlopen("https://blog.csdn.net/LOVEmy134611") as u:    f = io.TextIOWrapper(u, encoding = "utf-8")    text = f.read()print(text)

B.

import urllib.requestimport iowith urllib.request.urlopen("https://blog.csdn.net/LOVEmy134611") as u:    f = io.TextIOWrapper(u.read(), encoding = "utf-8")    text = f.read()print(text)

C.

import urllib.requestimport iowith urllib.request.urlopen("https://blog.csdn.net/LOVEmy134611") as u:    f = io.TextIOWrapper(u.detach(), encoding = "utf-8")    text = f.read()print(text)

D.

import urllib.requestimport iowith urllib.request.urlopen("https://blog.csdn.net/LOVEmy134611") as u:    f = io.TextIOWrapper(u).encoding="utf-8"    text = f.read()print(text)

正确答案:A

10. 第 10 题:临时文件与文件夹的创建

知识点描述:在程序执行时创建临时文件或目录,并在使用后自动销毁。
问题描述:创建一个命名临时文件,向文件中写入 “Hello Python!” 后打印文件名,请从以下选项中选出你认为正确的答案:
A.

from tempfile import NamedTemporaryFilewith NamedTemporaryFile("text.txt", "wt") as f:    f.write("Hello Python!/n")    print(f.name)

B.

from tempfile import TemporaryFilewith TemporaryFile("text.txt", "wt") as f:    f.write("Hello Python!/n")    print(f.name)

C.

from tempfile import NamedTemporaryFilewith NamedTemporaryFile("wt") as f:    f.write("Hello Python!/n")    print(f.name)

D.

from tempfile import TemporaryFilewith TemporaryFile("wt") as f:    f.write("Hello Python!/n")    print(f.name)

正确答案:C

试题代码地址

https://codechina.csdn.net/LOVEmy134611/python_problem

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/122017.html

相关文章

  • 仅需10道题轻松掌握Matplotlib图形处理 | Python技能征题

    摘要:问题描述绘制函数上的点,请从以下选项中选出你认为正确的答案正确答案第题条形图的绘制知识点描述绘制条形图。 仅需10道题轻松掌握Matplotlib图形处理 | P...

    YorkChen 评论0 收藏0
  • SciPy 积分 | Python技能征题

    摘要:积分技能树征题前言第题具有函数表达式的被积函数求积分第题函数表达式未知的积分求解试题代码地址前言积分在科学和工程应用中具有许多重要的应用,本文利用解决积分相关问题。 ...

    Code4App 评论0 收藏0
  • 学习Python:做数据科学还是网站开发?

    摘要:属于前一种,而且日益被用于数学计算机器学习和多种数据科学应用。近来,由于拥有多个针对机器学习自然语言处理数据视觉化数据探索数据分析和数据挖掘的插件,丰富的数据科学生态体系得到了较大的发展,甚至有将数据科学社区化的趋势。 译者注:本文的英文原文地址是:Python for Data Science vs Python for Web Development,发布时间是10月29日。译者一...

    neu 评论0 收藏0
  • 学习python就用python技能树|Python技能树测评

    摘要:你想学习吗你知道技能树吗技能树是提供的系统化,面向实战的学习环境。如果你是初学者请马上开始学习,你最终可以获得的技能认证。学习到任何一阶段的同学们都可以轻松加入技能树的学习,所以你要学习就请赶快加入吧。 python 是一种很流行的高级动态语言。编程语言的的排行可以参考TIOBE。当然如果从...

    Harpsichord1207 评论0 收藏0
  • 软件测试需要学什么?50W+的测试工程师需要掌握哪些技能

    摘要:协议学习常见请求方法学习和学习接口的基本概念接口文档认识接口测试用例编写接口测试工具使用软件测试自动化进阶性能测试性能测试的技术要求很高,不仅仅要对性能测试的指标测试分类测试设计有很深刻的理解。 ...

    wushuiyong 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<