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
Run a Process to /Dev/Null in Python
How to Kill Zombie Processes Created by Multiprocessing Module
(Still) Cannot Properly Install Lxml 2.3 for Python, But at Least 2.2.8 Works
Why am I Getting Socket.Gaierror: [Errno -2] from Python Httplib
How Can Python Handle Systemctl Stop
How to Get a Function Name as a String
How to Select Which Version of Python I am Running on Linux
Cannot Save Matplotlib Animation With Ffmpeg
How to Retrieve the Process Start Time (Or Uptime) in Python
Typeerror: Argument 1 Must Be Pygame.Surface, Not Str How to Fix
How to Rewrite Output in Terminal
Trouble Connecting to Phantomjs Webdriver Using Python and Selenium
Python 2.7 Cannot Import Pyqt4
Update Python on Linux 2.7 to 3.5