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

x33g5p2x  于2021-10-06 转载在 Python  
字(6.1k)|赞(0)|评价(0)|浏览(349)

0.前言

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

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

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

  1. import os
  2. path = '/home/brainiac/Documents/csdn/hello_world.py'
  3. file_name = os.path.dirname(path)
  4. print(file_name)

B.

  1. import os
  2. path = '/home/brainiac/Documents/csdn/hello_world.py'
  3. file_name = os.path.abspath(path)
  4. print(file_name)

C.

  1. import os
  2. path = '/home/brainiac/Documents/csdn/hello_world.py'
  3. file_name = os.path.basename(path)
  4. print(file_name)

D.

  1. string = '四十'
  2. result = string.isdigit()
  3. print(result)

正确答案: C

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

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

  1. import os
  2. exist = os.path.exists('etc/passwd')
  3. print(exist)

B.

  1. import os
  2. exist = os.path.isdir('etc/passwd')
  3. print(exist)

C.

  1. import os
  2. exist = os.path.isdir('/etc/passwd')
  3. print(exist)

D.

  1. import os
  2. exist = os.path.isfile('/etc/passwd')
  3. print(exist)

正确答案: D

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

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

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

B.

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

C.

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

D.

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

正确答案: B

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

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

  1. new_line_1 = "New line 1"
  2. new_line_2 = "New line 2"
  3. with open('text_1.txt', 'rt') as f:
  4. f.write(new_line_1+'\n')
  5. f.write(new_line_2+'\n')

B.

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

C.

  1. new_line_1 = "New line 1"
  2. new_line_2 = "New line 2"
  3. with open('text_1.txt', 'wt') as f:
  4. f.write(new_line_1+'\n')
  5. f.write(new_line_2+'\n')

D.

  1. new_line_1 = "New line 1"
  2. new_line_2 = "New line 2"
  3. with open('text_1.txt', 'at') as f:
  4. f.write(new_line_1+'\n')
  5. f.write(new_line_2+'\n')

正确答案: D

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

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

  1. from datetime import datetime
  2. def hello_world(num):
  3. return "Hello world {}!".format(num)
  4. for i in range(10):
  5. with open('log.txt', 'at') as f:
  6. print(str(datetime.today()) + '\t' + hello_world(i), file=f)

B.

  1. from datetime import datetime
  2. def hello_world(num):
  3. return "Hello world {}!".format(num)
  4. for i in range(10):
  5. with open('log.txt', 'at') as f:
  6. print(datetime.today() + '\t' + hello_world(i), file=f)

C.

  1. from datetime import datetime
  2. def hello_world(num):
  3. return "Hello world {}!".format(num)
  4. for i in range(10):
  5. with open('log.txt', 'wt') as f:
  6. print(datetime.today() + '\t' + hello_world(i))

D.

  1. from datetime import datetime
  2. def hello_world(num):
  3. return "Hello world {}!".format(num)
  4. for i in range(10):
  5. with open('log.txt', 'wt') as f:
  6. print(str(datetime.today()) + '\t' + hello_world(i), file=f)

正确答案: A

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

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

  1. with open('test.bin', 'at') as f:
  2. text = 'Hello World!\n'
  3. f.write(text)

B.

  1. with open('test.bin', 'wb') as f:
  2. text = 'Hello World!\n'
  3. f.write(text.encode('utf-8'))

C.

  1. with open('test.bin', 'ab') as f:
  2. text = 'Hello World!\n'
  3. f.write(text.encode('utf-8'))

D.

  1. with open('test.bin', 'ab') as f:
  2. text = 'Hello World!\n'
  3. f.write(text)

正确答案: C

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

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

  1. import gzip
  2. text = 'text.txt'
  3. with gzip.open('compress.gz', 'wt', compresslevel = 9) as f:
  4. f.write(text)

B.

  1. import gzip
  2. text = 'text.txt'
  3. with gzip.open('compress.gz', 'wt', compresslevel = 0) as f:
  4. f.write(text)

C.

  1. import gzip
  2. text = 'text.txt'
  3. with open(text, 'rt') as file:
  4. read_text = file.read()
  5. with gzip.open('compress.gz', 'wt', compresslevel = 9) as f:
  6. f.write(read_text)

D.

  1. import gzip
  2. text = 'text.txt'
  3. with open(text, 'rt') as file:
  4. read_text = file.read()
  5. with gzip.open('compress.gz', 'wt', compresslevel = 0) as f:
  6. f.write(read_text)

正确答案:C

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

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

  1. from functools import partial
  2. RECORD_SIZE = 16
  3. with open('test.bin', 'rt') as f:
  4. records = iter(partial(f.read, RECORD_SIZE), b'')
  5. for r in records:
  6. print(r)

B.

  1. from functools import partial
  2. RECORD_SIZE = 16
  3. with open('test.bin', 'rb') as f:
  4. records = iter(partial(f.read, RECORD_SIZE), b'')
  5. for r in records:
  6. print(r)

C.

  1. from functools import partial
  2. RECORD_SIZE = 16 * 8
  3. with open('test.bin', 'rt') as f:
  4. records = iter(partial(f.read, RECORD_SIZE), b'')
  5. for r in records:
  6. print(r)

D.

  1. from functools import partial
  2. RECORD_SIZE = 16
  3. with open('test.bin', 'rt') as f:
  4. records = iter(partial(f.read, RECORD_SIZE), '')
  5. for r in records:
  6. print(r)

正确答案:B

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

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

  1. import urllib.request
  2. import io
  3. with urllib.request.urlopen('https://blog.csdn.net/LOVEmy134611') as u:
  4. f = io.TextIOWrapper(u, encoding = 'utf-8')
  5. text = f.read()
  6. print(text)

B.

  1. import urllib.request
  2. import io
  3. with urllib.request.urlopen('https://blog.csdn.net/LOVEmy134611') as u:
  4. f = io.TextIOWrapper(u.read(), encoding = 'utf-8')
  5. text = f.read()
  6. print(text)

C.

  1. import urllib.request
  2. import io
  3. with urllib.request.urlopen('https://blog.csdn.net/LOVEmy134611') as u:
  4. f = io.TextIOWrapper(u.detach(), encoding = 'utf-8')
  5. text = f.read()
  6. print(text)

D.

  1. import urllib.request
  2. import io
  3. with urllib.request.urlopen('https://blog.csdn.net/LOVEmy134611') as u:
  4. f = io.TextIOWrapper(u).encoding='utf-8'
  5. text = f.read()
  6. print(text)

正确答案:A

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

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

  1. from tempfile import NamedTemporaryFile
  2. with NamedTemporaryFile('text.txt', 'wt') as f:
  3. f.write('Hello Python!\n')
  4. print(f.name)

B.

  1. from tempfile import TemporaryFile
  2. with TemporaryFile('text.txt', 'wt') as f:
  3. f.write('Hello Python!\n')
  4. print(f.name)

C.

  1. from tempfile import NamedTemporaryFile
  2. with NamedTemporaryFile('wt') as f:
  3. f.write('Hello Python!\n')
  4. print(f.name)

D.

  1. from tempfile import TemporaryFile
  2. with TemporaryFile('wt') as f:
  3. f.write('Hello Python!\n')
  4. print(f.name)

正确答案:C

试题代码地址

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

相关文章