0

I'm working on an application in Python 3, but what I want is to upload any type of file. e. g. in c function :

fopen() 

you can load any file type. Is there any way to do that? already tried with :

open () 

but it throws me errors:

UnicodeDecodeError:' charmap' code cant decode byte.

Help :(

JUAN ALVARES
  • 75
  • 11
  • 1
    You're opening non-text files in text mode. You need binary mode. – user2357112 Dec 06 '17 at 00:57
  • 1
    use `"b"` in `open(..., "rb")` to open in binary mode. – furas Dec 06 '17 at 00:58
  • 2
    Possible duplicate of [How to open and read a binary file in Python?](https://stackoverflow.com/questions/35000687/how-to-open-and-read-a-binary-file-in-python) – NH. Dec 06 '17 at 01:25

2 Answers2

0

f = open(file, mode='rb') opens file in binary mode. f.read() then returns the file contents as bytes.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
0

Command open() as default opens it text mode which tries to decode text to unicode.

If you have to open other file then better use binary mode.

Use "b" in open(filename, "rb") to do it.

Python 3 doc: open()

furas
  • 134,197
  • 12
  • 106
  • 148