Open an encoded file using the given mode and return a wrapped version providing transparent encoding/decoding. Note: The wrapped version will only accept the object format defined by the codecs, i.e. Unicode objects for most builtin codecs. Output is also codec dep
(filename, mode='r', encoding=None, errors='strict', buffering=-1)
| 884 | ### Shortcuts |
| 885 | |
| 886 | def open(filename, mode='r', encoding=None, errors='strict', buffering=-1): |
| 887 | """ Open an encoded file using the given mode and return |
| 888 | a wrapped version providing transparent encoding/decoding. |
| 889 | |
| 890 | Note: The wrapped version will only accept the object format |
| 891 | defined by the codecs, i.e. Unicode objects for most builtin |
| 892 | codecs. Output is also codec dependent and will usually be |
| 893 | Unicode as well. |
| 894 | |
| 895 | If encoding is not None, then the |
| 896 | underlying encoded files are always opened in binary mode. |
| 897 | The default file mode is 'r', meaning to open the file in read mode. |
| 898 | |
| 899 | encoding specifies the encoding which is to be used for the |
| 900 | file. |
| 901 | |
| 902 | errors may be given to define the error handling. It defaults |
| 903 | to 'strict' which causes ValueErrors to be raised in case an |
| 904 | encoding error occurs. |
| 905 | |
| 906 | buffering has the same meaning as for the builtin open() API. |
| 907 | It defaults to -1 which means that the default buffer size will |
| 908 | be used. |
| 909 | |
| 910 | The returned wrapped file object provides an extra attribute |
| 911 | .encoding which allows querying the used encoding. This |
| 912 | attribute is only available if an encoding was specified as |
| 913 | parameter. |
| 914 | """ |
| 915 | import warnings |
| 916 | warnings.warn("codecs.open() is deprecated. Use open() instead.", |
| 917 | DeprecationWarning, stacklevel=2) |
| 918 | |
| 919 | if encoding is not None and \ |
| 920 | 'b' not in mode: |
| 921 | # Force opening of the file in binary mode |
| 922 | mode = mode + 'b' |
| 923 | file = builtins.open(filename, mode, buffering) |
| 924 | if encoding is None: |
| 925 | return file |
| 926 | |
| 927 | try: |
| 928 | info = lookup(encoding) |
| 929 | srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors) |
| 930 | # Add attributes to simplify introspection |
| 931 | srw.encoding = encoding |
| 932 | return srw |
| 933 | except: |
| 934 | file.close() |
| 935 | raise |
| 936 | |
| 937 | def EncodedFile(file, data_encoding, file_encoding=None, errors='strict'): |
| 938 |
nothing calls this directly
no test coverage detected
searching dependent graphs…