所有应用程序都需要处理输入和输出,文件是用于获取输入和保存输出的常用载体,文件可以是文本文档、图片、程序等等,我们就通过 10
道 Python
编程题来掌握解决常见文件处理问题的方法吧!
知识点描述:使用路径名来获取文件名,目录名,绝对路径。
问题描述:有一文件路径如下:“/home/brainiac/Documents/csdn/hello_world.py”,请从以下选项中选出可以获取文件名 “hello_world.py” 的选项:
A.
import os
path = '/home/brainiac/Documents/csdn/hello_world.py'
file_name = os.path.dirname(path)
print(file_name)
B.
import os
path = '/home/brainiac/Documents/csdn/hello_world.py'
file_name = os.path.abspath(path)
print(file_name)
C.
import os
path = '/home/brainiac/Documents/csdn/hello_world.py'
file_name = os.path.basename(path)
print(file_name)
D.
string = '四十'
result = string.isdigit()
print(result)
正确答案: C
知识点描述:检测指定文件或目录是否存在。
问题描述:请从以下选项中选出可以检测 “/etc/passwd” 文件是否存在的选项:
A.
import os
exist = os.path.exists('etc/passwd')
print(exist)
B.
import os
exist = os.path.isdir('etc/passwd')
print(exist)
C.
import os
exist = os.path.isdir('/etc/passwd')
print(exist)
D.
import os
exist = os.path.isfile('/etc/passwd')
print(exist)
正确答案: D
知识点描述:获取文件系统中指定目录下的所有文件列表。
问题描述:获取 “/etc” 目录中所有 python 文件(以 “.py” 作为文件后缀)列表,请从以下选项中选出你认为正确的选项:
A.
import os
path = "/usr/lib/python3/dist-packages"
names = [name for name in os.listdir(path)]
print(names)
B.
import os
path = "/usr/lib/python3/dist-packages"
names = [name for name in os.listdir(path) if name.endswith('.py')]
print(names)
C.
import os
path = "/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 os
path = "/usr/lib/python3/dist-packages"
names = [name for name in os.listdir(path) if name.endswith('*.py')]
print(names)
正确答案: B
知识点描述:读写使用不同编码方式的文本文件。
问题描述:假设存在一文件 “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
知识点描述:将 print() 函数的输出重定向到指定日志文件中。
问题描述:将当前时间写入日志文件 “log.txt” 中,并记录函数执行结果,请从以下选项中选出你认为正确的答案:
A.
from datetime import datetime
def 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 datetime
def 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 datetime
def 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 datetime
def 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
知识点描述:读写二进制文件,如图片、声音文件等。
问题描述:已知存在二进制文件 “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
知识点描述:读写 gzip 或 bz2 格式的压缩文件。
问题描述:请从以下选项中选择能够将文本文件 “text.txt” 内容写入压缩文件 “compress.gz” 的程序,且要求压缩程度最佳:
A.
import gzip
text = 'text.txt'
with gzip.open('compress.gz', 'wt', compresslevel = 9) as f:
f.write(text)
B.
import gzip
text = 'text.txt'
with gzip.open('compress.gz', 'wt', compresslevel = 0) as f:
f.write(text)
C.
import gzip
text = '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 gzip
text = '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
知识点描述:以固定长度数据块长度迭代读取文件,而非逐行读取。
问题描述:存在一文件 “test.bin”,编写程序每次读取数据块大小为 16B,直到文件末尾:
A.
from functools import partial
RECORD_SIZE = 16
with open('test.bin', 'rt') as f:
records = iter(partial(f.read, RECORD_SIZE), b'')
for r in records:
print(r)
B.
from functools import partial
RECORD_SIZE = 16
with open('test.bin', 'rb') as f:
records = iter(partial(f.read, RECORD_SIZE), b'')
for r in records:
print(r)
C.
from functools import partial
RECORD_SIZE = 16 * 8
with open('test.bin', 'rt') as f:
records = iter(partial(f.read, RECORD_SIZE), b'')
for r in records:
print(r)
D.
from functools import partial
RECORD_SIZE = 16
with open('test.bin', 'rt') as f:
records = iter(partial(f.read, RECORD_SIZE), '')
for r in records:
print(r)
正确答案:B
知识点描述:在不关闭已打开文件前提下改变文件的编码方式。
问题描述:如何为一个以二进制模式打开的文件添加 “utf-8” 编码方式,请从以下选项中选出你认为正确的答案:
A.
import urllib.request
import io
with 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.request
import io
with 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.request
import io
with 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.request
import io
with urllib.request.urlopen('https://blog.csdn.net/LOVEmy134611') as u:
f = io.TextIOWrapper(u).encoding='utf-8'
text = f.read()
print(text)
正确答案:A
知识点描述:在程序执行时创建临时文件或目录,并在使用后自动销毁。
问题描述:创建一个命名临时文件,向文件中写入 “Hello Python!” 后打印文件名,请从以下选项中选出你认为正确的答案:
A.
from tempfile import NamedTemporaryFile
with NamedTemporaryFile('text.txt', 'wt') as f:
f.write('Hello Python!\n')
print(f.name)
B.
from tempfile import TemporaryFile
with TemporaryFile('text.txt', 'wt') as f:
f.write('Hello Python!\n')
print(f.name)
C.
from tempfile import NamedTemporaryFile
with NamedTemporaryFile('wt') as f:
f.write('Hello Python!\n')
print(f.name)
D.
from tempfile import TemporaryFile
with TemporaryFile('wt') as f:
f.write('Hello Python!\n')
print(f.name)
正确答案:C
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/LOVEmy134611/article/details/120613099
内容来源于网络,如有侵权,请联系作者删除!