Python return os.system and subprocess output as a string

When we want to use Unix command in python we can directly use os.system() to realize it. However, if we only want to return the output as a string, for example, return ls file names into a string, we need to use subprocess.check_out instead.

To note that, the output from subprocess.check_out() is a bytes object instead of a string, thus we need to further decode to transform into a string.

fileList = subprocess.check_output('ls someFolder/*', shell=True).decode('utf-8').strip().split('\n')


Substitute ‘\r’ in Excel output to ‘\n’ for Unix

When outputing tsv from Excel directly, it added an unpleasant ‘\r’ at the end of each line. And you won’t even notive it by opening the file in TextWrangler. It can be checked by head -n 2 input.txt, you will see the difference.

Here is how to replace the ‘\r’ with ‘\n’:

cat input.txt | tr '\r' '\n' > output.txt
cat input.txt | tr '^M' '\n' > output.txt

While typing ^M in the command, do not use shift+6 & M. You should use ctrl+v+m