僅需10道題輕松掌握Python檔案處理 | Python技能樹征題
- 0.前言
- 1. 第 1 題:檔案路徑名的處理
- 2. 第 2 題:檢測檔案是否存在
- 3. 第 3 題:獲取指定檔案夾下的檔案串列
- 4. 第 4 題:文本檔案的讀寫
- 5. 第 5 題:將列印輸出到檔案中
- 6. 第 6 題:二進制檔案的讀寫
- 7. 第 7 題:壓縮檔案的讀寫
- 8. 第 8 題:以固定資料塊大小讀取檔案
- 9. 第 9 題:增加或改變已打開檔案的編碼方式
- 10. 第 10 題:臨時檔案與檔案夾的創建
- 試題代碼地址
0.前言
所有應用程式都需要處理輸入和輸出,檔案是用于獲取輸入和保存輸出的常用載體,檔案可以是文本檔案、圖片、程式等等,我們就通過 10 道 Python 編程題來掌握解決常見檔案處理問題的方法吧!
1. 第 1 題:檔案路徑名的處理
知識點描述:使用路徑名來獲取檔案名,目錄名,絕對路徑,
問題描述:有一檔案路徑如下:“/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
2. 第 2 題:檢測檔案是否存在
知識點描述:檢測指定檔案或目錄是否存在,
問題描述:請從以下選項中選出可以檢測 “/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
3. 第 3 題:獲取指定檔案夾下的檔案串列
知識點描述:獲取檔案系統中指定目錄下的所有檔案串列,
問題描述:獲取 “/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
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 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
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 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
8. 第 8 題:以固定資料塊大小讀取檔案
知識點描述:以固定長度資料塊長度迭代讀取檔案,而非逐行讀取,
問題描述:存在一檔案 “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
9. 第 9 題:增加或改變已打開檔案的編碼方式
知識點描述:在不關閉已打開檔案前提下改變檔案的編碼方式,
問題描述:如何為一個以二進制模式打開的檔案添加 “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
10. 第 10 題:臨時檔案與檔案夾的創建
知識點描述:在程式執行時創建臨時檔案或目錄,并在使用后自動銷毀,
問題描述:創建一個命名臨時檔案,向檔案中寫入 “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://codechina.csdn.net/LOVEmy134611/python_problem
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/305985.html
標籤:python
