MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / FileIO

Class FileIO

tensorflow/python/lib/io/file_io.py:42–245  ·  view source on GitHub ↗

FileIO class that exposes methods to read / write to / from files. The constructor takes the following arguments: name: name of the file mode: one of 'r', 'w', 'a', 'r+', 'w+', 'a+'. Append 'b' for bytes mode. Can be used as an iterator to iterate over lines in the file. The default buf

Source from the content-addressed store, hash-verified

40
41
42class FileIO(object):
43 """FileIO class that exposes methods to read / write to / from files.
44
45 The constructor takes the following arguments:
46 name: name of the file
47 mode: one of 'r', 'w', 'a', 'r+', 'w+', 'a+'. Append 'b' for bytes mode.
48
49 Can be used as an iterator to iterate over lines in the file.
50
51 The default buffer size used for the BufferedInputStream used for reading
52 the file line by line is 1024 * 512 bytes.
53 """
54
55 def __init__(self, name, mode):
56 self.__name = name
57 self.__mode = mode
58 self._read_buf = None
59 self._writable_file = None
60 self._binary_mode = "b" in mode
61 mode = mode.replace("b", "")
62 if mode not in ("r", "w", "a", "r+", "w+", "a+"):
63 raise errors.InvalidArgumentError(
64 None, None, "mode is not 'r' or 'w' or 'a' or 'r+' or 'w+' or 'a+'")
65 self._read_check_passed = mode in ("r", "r+", "a+", "w+")
66 self._write_check_passed = mode in ("a", "w", "r+", "a+", "w+")
67
68 @property
69 def name(self):
70 """Returns the file name."""
71 return self.__name
72
73 @property
74 def mode(self):
75 """Returns the mode in which the file was opened."""
76 return self.__mode
77
78 def _preread_check(self):
79 if not self._read_buf:
80 if not self._read_check_passed:
81 raise errors.PermissionDeniedError(None, None,
82 "File isn't open for reading")
83 self._read_buf = pywrap_tensorflow.CreateBufferedInputStream(
84 compat.as_bytes(self.__name), 1024 * 512)
85
86 def _prewrite_check(self):
87 if not self._writable_file:
88 if not self._write_check_passed:
89 raise errors.PermissionDeniedError(None, None,
90 "File isn't open for writing")
91 self._writable_file = pywrap_tensorflow.CreateWritableFile(
92 compat.as_bytes(self.__name), compat.as_bytes(self.__mode))
93
94 def _prepare_value(self, val):
95 if self._binary_mode:
96 return compat.as_bytes(val)
97 else:
98 return compat.as_str_any(val)
99

Callers 4

read_file_to_stringFunction · 0.85
write_string_to_fileFunction · 0.85
filecmpFunction · 0.85
file_crc32Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected