file.txt 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. The ``File`` object
  2. ===================
  3. The :mod:`django.core.files` module and its submodules contain built-in classes
  4. for basic file handling in Django.
  5. .. currentmodule:: django.core.files
  6. The ``File`` Class
  7. ------------------
  8. .. class:: File(file_object)
  9. The :class:`File` class is a thin wrapper around a Python
  10. :py:term:`file object` with some Django-specific additions.
  11. Internally, Django uses this class when it needs to represent a file.
  12. :class:`File` objects have the following attributes and methods:
  13. .. attribute:: name
  14. The name of the file including the relative path from
  15. :setting:`MEDIA_ROOT`.
  16. .. attribute:: size
  17. The size of the file in bytes.
  18. .. attribute:: file
  19. The underlying :py:term:`file object` that this class wraps.
  20. .. attribute:: mode
  21. The read/write mode for the file.
  22. .. method:: open(mode=None)
  23. Open or reopen the file (which also does ``File.seek(0)``).
  24. The ``mode`` argument allows the same values
  25. as Python's built-in :func:`python:open()`.
  26. When reopening a file, ``mode`` will override whatever mode the file
  27. was originally opened with; ``None`` means to reopen with the original
  28. mode.
  29. .. method:: read(num_bytes=None)
  30. Read content from the file. The optional ``size`` is the number of
  31. bytes to read; if not specified, the file will be read to the end.
  32. .. method:: __iter__()
  33. Iterate over the file yielding one line at a time.
  34. .. versionchanged:: 1.8
  35. ``File`` now uses `universal newlines`_. The following are
  36. recognized as ending a line: the Unix end-of-line convention
  37. ``'\n'``, the Windows convention ``'\r\n'``, and the old Macintosh
  38. convention ``'\r'``.
  39. .. _universal newlines: https://www.python.org/dev/peps/pep-0278
  40. .. method:: chunks(chunk_size=None)
  41. Iterate over the file yielding "chunks" of a given size. ``chunk_size``
  42. defaults to 64 KB.
  43. This is especially useful with very large files since it allows them to
  44. be streamed off disk and avoids storing the whole file in memory.
  45. .. method:: multiple_chunks(chunk_size=None)
  46. Returns ``True`` if the file is large enough to require multiple chunks
  47. to access all of its content give some ``chunk_size``.
  48. .. method:: write(content)
  49. Writes the specified content string to the file. Depending on the
  50. storage system behind the scenes, this content might not be fully
  51. committed until :func:`close()` is called on the file.
  52. .. method:: close()
  53. Close the file.
  54. In addition to the listed methods, :class:`~django.core.files.File` exposes
  55. the following attributes and methods of its ``file`` object:
  56. ``encoding``, ``fileno``, ``flush``, ``isatty``, ``newlines``,
  57. ``read``, ``readinto``, ``readlines``, ``seek``, ``softspace``, ``tell``,
  58. ``truncate``, ``writelines``, ``xreadlines``. If you are using
  59. Python 3, the ``seekable`` method is also available.
  60. .. versionchanged:: 1.9
  61. The ``seekable`` method was added.
  62. .. currentmodule:: django.core.files.base
  63. The ``ContentFile`` Class
  64. -------------------------
  65. .. class:: ContentFile(File)
  66. The ``ContentFile`` class inherits from :class:`~django.core.files.File`,
  67. but unlike :class:`~django.core.files.File` it operates on string content
  68. (bytes also supported), rather than an actual file. For example::
  69. from __future__ import unicode_literals
  70. from django.core.files.base import ContentFile
  71. f1 = ContentFile("esta sentencia está en español")
  72. f2 = ContentFile(b"these are bytes")
  73. .. currentmodule:: django.core.files.images
  74. The ``ImageFile`` Class
  75. -----------------------
  76. .. class:: ImageFile(file_object)
  77. Django provides a built-in class specifically for images.
  78. :class:`django.core.files.images.ImageFile` inherits all the attributes
  79. and methods of :class:`~django.core.files.File`, and additionally
  80. provides the following:
  81. .. attribute:: width
  82. Width of the image in pixels.
  83. .. attribute:: height
  84. Height of the image in pixels.
  85. .. currentmodule:: django.core.files
  86. Additional methods on files attached to objects
  87. -----------------------------------------------
  88. Any :class:`File` that is associated with an object (as with ``Car.photo``,
  89. below) will also have a couple of extra methods:
  90. .. method:: File.save(name, content, save=True)
  91. Saves a new file with the file name and contents provided. This will not
  92. replace the existing file, but will create a new file and update the object
  93. to point to it. If ``save`` is ``True``, the model's ``save()`` method will
  94. be called once the file is saved. That is, these two lines::
  95. >>> car.photo.save('myphoto.jpg', content, save=False)
  96. >>> car.save()
  97. are equivalent to::
  98. >>> car.photo.save('myphoto.jpg', content, save=True)
  99. Note that the ``content`` argument must be an instance of either
  100. :class:`File` or of a subclass of :class:`File`, such as
  101. :class:`~django.core.files.base.ContentFile`.
  102. .. method:: File.delete(save=True)
  103. Removes the file from the model instance and deletes the underlying file.
  104. If ``save`` is ``True``, the model's ``save()`` method will be called once
  105. the file is deleted.