Python 3 and Python 2 differences notes

Some notes on Python 3 and Python 2 differences I have run into.

  • No more mix of tabs and spaces: Python 3 disallows mixing of tabs and white spaces. Python3 prefers white spaces for indentation.
  • The return of proc.stdout.readlines() is a byte array, that requires .decode() to turn into a string. Following code (credit Ajay) does an instance check on “line”.
if isinstance(command, list):
    proc = subprocess.Popen(command, stdout=subprocess.PIPE)
else:
    proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
proc.wait()
for line in proc.stdout.readlines():
    if isinstance(line, bytes):
        line = line.decode()
    output += line.rstrip() + "\n"
proc.stdout.close()
return output

 

 

 

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s