Linux - Weird Python Output

Linux - Weird Python Output

Assuming you are using ubuntu, here is the relevant bug report https://bugs.launchpad.net/ubuntu/+source/python3.3/+bug/1192890

You need to patch your /etc/bash.bashrc. See comment #6 for details

Why is printf() giving a strange output in python?

Those extra '1's at the end of each number are the return value from printf, which returns the number of chars that it prints. The return value of a function called in the interactive interpreter is automatically printed (unless it's None).

In fact, the interactive interpreter prints any non-None expression that isn't assigned. And of course it adds a newline to those expressions, which explains why the output in your first code block is on separate lines.

Your pr function doesn't have a return statement, so it returns None, and thus no extra stuff gets printed.

Weird newline character error in python

This is because the print function automatically adds a newline character at the end (ascii 0x0a). You can use the Python 3 version of print to set the end character:

> python3 -c "print('\x11\x22\x33\x44', end='')" > new_file
> hexdump -C new_file
11 22 33 44

If you really need to use Python 2, you can use this trick to run a multiline Python code in your terminal:

echo -e "import sys\nsys.stdout.write('\x11\x22\x33\x44')" | python > new_file

Python fabric sudo() returning weird escape characters in output

As per Fabric's changelog:

As part of the changes made in #7, run and sudo have had the default value of their pty kwargs changed from False to True. This, plus the addition of the combine_stderr kwarg/env var, may result in significant behavioral changes in remote programs which operate differently when attached to a tty.

Basically from 0.9 to 1.x default value has been swapped, hence your output difference.

Python behaves weird with piped input

It is a carriage-return and line-feed thing, but you have not provided enough clues to determine the exact problem.

Experiment by adding the following to the end of your strings before using them in a print statement.
+'\r'
+'\n'

Inspect your files with a hex editor after reading https://blog.codinghorror.com/the-great-newline-schism/.

Check how your console, aka terminal window, aka command line, is configured to handle these control characters. Are you using the default settings?

You must consistently use these control characters, as per the conventions of your operating system.

It would complicate matters further if you are using telnet or ssh on for example a Microsoft operating system to run code on for example a Unix computer.



Related Topics



Leave a reply



Submit