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

網(wǎng)站建設哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、微信小程序、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了從江免費建站歡迎大家使用!
大部分情況下你只需要使用 Django 提供的 File
如果你需要自己構建 File ,最簡單的方法是使用 Python 內置的 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)建的文件不會自動關閉。下面的方式可以用來自動關閉文件:
>>> 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在對大量對象進行循環(huán)訪問文件字段時,關閉文件尤為重要。如果文件在訪問后不能手動關閉,可能會出現(xiàn)文件描述符溢出的風險。
OSError: [Errno 24] Too many open files 本文名稱:創(chuàng)新互聯(lián)Django4.0教程:Django4.0管理文件-File對象
本文鏈接:http://fisionsoft.com.cn/article/cdocjhh.html


咨詢
建站咨詢
