Sufiyan Ghori
Sufiyan Ghori

Reputation: 18753

string is not same in Python3x as for Python2.x but Still it is working

I am confused with the strings in Python,

Consider this code,

fo = open("test.txt", "wb")
fo.write(bytes('Sufiyan','UTF-8'))

If I were to run this code, It would give this error,

Traceback (most recent call last):

  File "C:\Users\Sufiyan\Desktop\AES\a.py", line 2, in <module>

    fo.write('Sufiyan')

TypeError: 'str' does not support the buffer interface

Therefore, I have to convert it into bytes and provide the encoding type.

fo = open("test.txt", "wb")
fo.write(bytes('Sufiyan','UTF-8'))

This works, because If you use Python3x then string is not the same type as for Python 2.x, you must cast it to bytes (encode it).

Now, When I am using this code to write to a file,

def BB_cf(file, *args, **kwargs): #Create files
    
    try:
        
        fo = open(file)
        fo.close()
        
    except IOError:

        print (file, 'not Found')
        print ("Creating file.....")
        fo = open(file, "w")
        print (file,"Created Successfully!")
        
        if file == 'input_Data.txt':
            print(file,"is Empty..\nWriting Data..")
            text = kwargs.get('text', None)
            fo.write(text)
            print("'"+text+"'", "is written in",file)
            fo.close()
            
        fo.close()

.

BB_cf('input_Data.txt', text='Sufiyan Ghori')

As you can see in the 4th last line, fo.write(text), I haven't encoded it, but the code is still working.

Why the code is working without encoding it ?

Now if I cast it to bytes, It will give the following error,

Traceback (most recent call last):

File "C:\Users\Sufiyan\Desktop\AES\BlackBox.py", line 47, in

BB_cf('input_Data.txt', text='Sufiyan Ghori')   File 

"C:\Users\Sufiyan\Desktop\AES\BlackBox.py", line 41, in BB_cf

fo.write(bytes(text,'UTF-8')) TypeError: must be str, not bytes

Both the above codes are running using Python3x, The first one wants me to encode the string into Bytes while the 2nd one is running without encoding.

Upvotes: 1

Views: 76

Answers (1)

Michael0x2a
Michael0x2a

Reputation: 64118

The first time around, you open your file by doing fo = open("test.txt", "wb"). The second time, you do fo = open(file, "w"). The string "wb" indicates to Python that you want to write to the file using only bytes, instead of strings, whereas the string "w" in your second example states the reverse.

Specifically, the documentation for the open function states:

As mentioned in the Overview, Python distinguishes between binary and text I/O. Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding. In text mode (the default, or when 't' is included in the mode argument), the contents of the file are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.

(emphasis added by me)

Upvotes: 2

Related Questions