简体   繁体   中英

Python 3.6: can't concat str to bytes

I have a problem in reading text files in python 3.6.3.

The code is given below:

def read_docs(doc):
    files=glob.glob(doc)


    for var in files:
       with open(var,'r', encoding='utf-8') as c1:
          a1=reader(c1,'ignore').read()

I am getting the following error:

TypeError: can't concat str to bytes.

I have seen similar questions but I am not clear about how to deal with it. I am stuck as I could not proceed to the application without reading the text files.

Any suggestions is appreciated.

Thanks in advance.

Try change file mode to "rb" like this open(var, 'rb', encoding='utf-8') . File mode "rb" returns bytes. File mode "r" returns string.

Check documentation about this function here: https://docs.python.org/3.6/library/functions.html#open

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM