subprocess and Type Str doesnt support the buffer API
Question: I have

    
    
    cmd = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
    for line in cmd.stdout:
      columns = line.split(' ')
      print (columns[3])
    

have error in line 3 Type Str doesnt support the buffer API.

What am i doing wrong i am on Python 3.3


Answer: You are reading binary data, not `str`, so you need to decode the output
first:

    
    
    import locale
    
    encoding = locale.getdefaultlocale()[1]
    
    cmd = subprocess.Popen('dir',shell=True,stdout=subprocess.PIPE)
    for line in cmd.stdout:
        columns = line.decode(encoding).split()
        if columns:
            print(columns[-1])
    

You were trying to split a `bytes` value using a `str` argument:

    
    
    >>> b'one two'.split(' ')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Type str doesn't support the buffer API
    

By decoding you avoid that problem, and your `print()` call will not have to
prepend the output with `b'..'` either.

However, you probably just want to use the `os` module instead to get
filesystem information:

    
    
    import os
    
    for filename in os.listdir('.'):
        print(filename)
    

