成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

僅需10道題輕松掌握Python文件處理 | Python技能樹(shù)征題

caoym / 3380人閱讀

摘要:?jiǎn)栴}描述已知存在二進(jìn)制文件,如何正確向此文件追加寫(xiě)入文本數(shù)據(jù),請(qǐng)從以下選項(xiàng)中選出你認(rèn)為正確的答案正確答案第題壓縮文件的讀寫(xiě)知識(shí)點(diǎn)描述讀寫(xiě)或格式的壓縮文件。

0.前言

所有應(yīng)用程序都需要處理輸入和輸出,文件是用于獲取輸入和保存輸出的常用載體,文件可以是文本文檔、圖片、程序等等,我們就通過(guò) 10Python 編程題來(lái)掌握解決常見(jiàn)文件處理問(wèn)題的方法吧!

1. 第 1 題:文件路徑名的處理

知識(shí)點(diǎn)描述:使用路徑名來(lái)獲取文件名,目錄名,絕對(duì)路徑。
問(wèn)題描述:有一文件路徑如下:“/home/brainiac/Documents/csdn/hello_world.py”,請(qǐng)從以下選項(xiàng)中選出可以獲取文件名 “hello_world.py” 的選項(xiàng):
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 題:檢測(cè)文件是否存在

知識(shí)點(diǎn)描述:檢測(cè)指定文件或目錄是否存在。
問(wèn)題描述:請(qǐng)從以下選項(xiàng)中選出可以檢測(cè) “/etc/passwd” 文件是否存在的選項(xiàng):
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 題:獲取指定文件夾下的文件列表

知識(shí)點(diǎn)描述:獲取文件系統(tǒng)中指定目錄下的所有文件列表。
問(wèn)題描述:獲取 “/etc” 目錄中所有 python 文件(以 “.py” 作為文件后綴)列表,請(qǐng)從以下選項(xiàng)中選出你認(rèn)為正確的選項(xiàng):
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 題:文本文件的讀寫(xiě)

知識(shí)點(diǎn)描述:讀寫(xiě)使用不同編碼方式的文本文件。
問(wèn)題描述:假設(shè)存在一文件 “text_1.txt”,如何向其中再添加兩行新數(shù)據(jù),請(qǐng)從以下選項(xiàng)中選出你認(rèn)為正確的選項(xiàng):
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 題:將打印輸出到文件中

知識(shí)點(diǎn)描述:將 print() 函數(shù)的輸出重定向到指定日志文件中。
問(wèn)題描述:將當(dāng)前時(shí)間寫(xiě)入日志文件 “l(fā)og.txt” 中,并記錄函數(shù)執(zhí)行結(jié)果,請(qǐng)從以下選項(xiàng)中選出你認(rèn)為正確的答案:
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 題:二進(jìn)制文件的讀寫(xiě)

知識(shí)點(diǎn)描述:讀寫(xiě)二進(jìn)制文件,如圖片、聲音文件等。
問(wèn)題描述:已知存在二進(jìn)制文件 “test.bin”,如何正確向此文件追加寫(xiě)入文本數(shù)據(jù),請(qǐng)從以下選項(xiàng)中選出你認(rèn)為正確的答案:
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 題:壓縮文件的讀寫(xiě)

知識(shí)點(diǎn)描述:讀寫(xiě) gzip 或 bz2 格式的壓縮文件。
問(wèn)題描述:請(qǐng)從以下選項(xiàng)中選擇能夠?qū)⑽谋疚募?“text.txt” 內(nèi)容寫(xiě)入壓縮文件 “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 題:以固定數(shù)據(jù)塊大小讀取文件

知識(shí)點(diǎn)描述:以固定長(zhǎng)度數(shù)據(jù)塊長(zhǎng)度迭代讀取文件,而非逐行讀取。
問(wèn)題描述:存在一文件 “test.bin”,編寫(xiě)程序每次讀取數(shù)據(jù)塊大小為 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 題:增加或改變已打開(kāi)文件的編碼方式

知識(shí)點(diǎn)描述:在不關(guān)閉已打開(kāi)文件前提下改變文件的編碼方式。
問(wèn)題描述:如何為一個(gè)以二進(jìn)制模式打開(kāi)的文件添加 “utf-8” 編碼方式,請(qǐng)從以下選項(xiàng)中選出你認(rèn)為正確的答案:
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 題:臨時(shí)文件與文件夾的創(chuàng)建

知識(shí)點(diǎn)描述:在程序執(zhí)行時(shí)創(chuàng)建臨時(shí)文件或目錄,并在使用后自動(dòng)銷毀。
問(wèn)題描述:創(chuàng)建一個(gè)命名臨時(shí)文件,向文件中寫(xiě)入 “Hello Python!” 后打印文件名,請(qǐng)從以下選項(xiàng)中選出你認(rèn)為正確的答案:
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

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/122017.html

相關(guān)文章

  • 僅需10道題輕松掌握Matplotlib圖形處理 | Python技能樹(shù)征題

    摘要:?jiǎn)栴}描述繪制函數(shù)上的點(diǎn),請(qǐng)從以下選項(xiàng)中選出你認(rèn)為正確的答案正確答案第題條形圖的繪制知識(shí)點(diǎn)描述繪制條形圖。 僅需10道題輕松掌握Matplotlib圖形處理 | P...

    YorkChen 評(píng)論0 收藏0
  • SciPy 積分 | Python技能樹(shù)征題

    摘要:積分技能樹(shù)征題前言第題具有函數(shù)表達(dá)式的被積函數(shù)求積分第題函數(shù)表達(dá)式未知的積分求解試題代碼地址前言積分在科學(xué)和工程應(yīng)用中具有許多重要的應(yīng)用,本文利用解決積分相關(guān)問(wèn)題。 ...

    Code4App 評(píng)論0 收藏0
  • 學(xué)習(xí)Python:做數(shù)據(jù)科學(xué)還是網(wǎng)站開(kāi)發(fā)?

    摘要:屬于前一種,而且日益被用于數(shù)學(xué)計(jì)算機(jī)器學(xué)習(xí)和多種數(shù)據(jù)科學(xué)應(yīng)用。近來(lái),由于擁有多個(gè)針對(duì)機(jī)器學(xué)習(xí)自然語(yǔ)言處理數(shù)據(jù)視覺(jué)化數(shù)據(jù)探索數(shù)據(jù)分析和數(shù)據(jù)挖掘的插件,豐富的數(shù)據(jù)科學(xué)生態(tài)體系得到了較大的發(fā)展,甚至有將數(shù)據(jù)科學(xué)社區(qū)化的趨勢(shì)。 譯者注:本文的英文原文地址是:Python for Data Science vs Python for Web Development,發(fā)布時(shí)間是10月29日。譯者一...

    neu 評(píng)論0 收藏0
  • 學(xué)習(xí)python就用python技能樹(shù)|Python技能樹(shù)測(cè)評(píng)

    摘要:你想學(xué)習(xí)嗎你知道技能樹(shù)嗎技能樹(shù)是提供的系統(tǒng)化,面向?qū)崙?zhàn)的學(xué)習(xí)環(huán)境。如果你是初學(xué)者請(qǐng)馬上開(kāi)始學(xué)習(xí),你最終可以獲得的技能認(rèn)證。學(xué)習(xí)到任何一階段的同學(xué)們都可以輕松加入技能樹(shù)的學(xué)習(xí),所以你要學(xué)習(xí)就請(qǐng)趕快加入吧。 python 是一種很流行的高級(jí)動(dòng)態(tài)語(yǔ)言。編程語(yǔ)言的的排行可以參考TIOBE。當(dāng)然如果從...

    Harpsichord1207 評(píng)論0 收藏0
  • 軟件測(cè)試需要學(xué)什么?50W+的測(cè)試工程師需要掌握哪些技能

    摘要:協(xié)議學(xué)習(xí)常見(jiàn)請(qǐng)求方法學(xué)習(xí)和學(xué)習(xí)接口的基本概念接口文檔認(rèn)識(shí)接口測(cè)試用例編寫(xiě)接口測(cè)試工具使用軟件測(cè)試自動(dòng)化進(jìn)階性能測(cè)試性能測(cè)試的技術(shù)要求很高,不僅僅要對(duì)性能測(cè)試的指標(biāo)測(cè)試分類測(cè)試設(shè)計(jì)有很深刻的理解。 ...

    wushuiyong 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<