git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

python2 vs python3



On 18 Oct 2019 20:36, Chris Angelico <rosuav at gmail.com> wrote:

On Sat, Oct 19, 2019 at 5:29 AM Jagga Soorma <jagga13 at gmail.com> wrote:
>
> Hello,
>
> I am writing my second python script and got it to work using
> python2.x.  However, realized that I should be using python3 and it
> seems to fail with the following message:
>
> --
> Traceback (most recent call last):
>   File "test_script.py", line 29, in <module>
>     test_cmd = ("diskcmd -u " + x + " | grep -v '\*' | awk '{print $1,
> $3, $4, $9, $10}'" )
> TypeError: Can't convert 'bytes' object to str implicitly
> --
>
> I then run this command and save the output like this:
>
> --
> test_info = (subprocess.check_output( test_cmd,
> stderr=subprocess.STDOUT, shell=True )).splitlines()
> --
>
> Looks like the command output is in bytes and I can't simply wrap that
> around str().  Thanks in advance for your help with this.

>That's correct. The output of the command >is, by default, given to you
>in bytes.

Do you happen to know why this is the default?  And is there a reliable way to figure out the encoding? On posix, it's probably utf8, but on windows I usually use cp437, but knowing windows, it could be any codepage (you can even change it with chcp.exe)