Open `path` with `mode` and return the file object. If ``path`` is an URL, it will be downloaded, stored in the `DataSource` `destpath` directory and opened from there. Parameters ---------- path : str Local file path or URL to open. mode : str, optional
(path, mode='r', destpath=os.curdir, encoding=None, newline=None)
| 153 | _file_openers = _FileOpeners() |
| 154 | |
| 155 | def open(path, mode='r', destpath=os.curdir, encoding=None, newline=None): |
| 156 | """ |
| 157 | Open `path` with `mode` and return the file object. |
| 158 | |
| 159 | If ``path`` is an URL, it will be downloaded, stored in the |
| 160 | `DataSource` `destpath` directory and opened from there. |
| 161 | |
| 162 | Parameters |
| 163 | ---------- |
| 164 | path : str |
| 165 | Local file path or URL to open. |
| 166 | mode : str, optional |
| 167 | Mode to open `path`. Mode 'r' for reading, 'w' for writing, 'a' to |
| 168 | append. Available modes depend on the type of object specified by |
| 169 | path. Default is 'r'. |
| 170 | destpath : str, optional |
| 171 | Path to the directory where the source file gets downloaded to for |
| 172 | use. If `destpath` is None, a temporary directory will be created. |
| 173 | The default path is the current directory. |
| 174 | encoding : {None, str}, optional |
| 175 | Open text file with given encoding. The default encoding will be |
| 176 | what `io.open` uses. |
| 177 | newline : {None, str}, optional |
| 178 | Newline to use when reading text file. |
| 179 | |
| 180 | Returns |
| 181 | ------- |
| 182 | out : file object |
| 183 | The opened file. |
| 184 | |
| 185 | Notes |
| 186 | ----- |
| 187 | This is a convenience function that instantiates a `DataSource` and |
| 188 | returns the file object from ``DataSource.open(path)``. |
| 189 | |
| 190 | """ |
| 191 | |
| 192 | ds = DataSource(destpath) |
| 193 | return ds.open(path, mode, encoding=encoding, newline=newline) |
| 194 | |
| 195 | |
| 196 | @set_module('numpy') |