新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Django4.0教程:Django4.0管理文件-File對象
在內(nèi)部,Django 在任何需要表示文件的時候使用 ?django.core.files.File?

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了從江免費(fèi)建站歡迎大家使用!
大部分情況下你只需要使用 Django 提供的 File
如果你需要自己構(gòu)建 File ,最簡單的方法是使用 Python 內(nèi)置的 file 對象創(chuàng)建一個:
>>> from django.core.files import File
# Create a Python file object using open()
>>> f = open('/path/to/hello.world', 'w')
>>> myfile = File(f)現(xiàn)在你可以使用 File 類的任何屬性和方法。
注意在這里創(chuàng)建的文件不會自動關(guān)閉。下面的方式可以用來自動關(guān)閉文件:
>>> from django.core.files import File
# Create a Python file object using open() and the with statement
>>> with open('/path/to/hello.world', 'w') as f:
... myfile = File(f)
... myfile.write('Hello World')
...
>>> myfile.closed
True
>>> f.closed
True在對大量對象進(jìn)行循環(huán)訪問文件字段時,關(guān)閉文件尤為重要。如果文件在訪問后不能手動關(guān)閉,可能會出現(xiàn)文件描述符溢出的風(fēng)險。
OSError: [Errno 24] Too many open files 當(dāng)前題目:創(chuàng)新互聯(lián)Django4.0教程:Django4.0管理文件-File對象
瀏覽地址:http://www.5511xx.com/article/cdocjhh.html


咨詢
建站咨詢
