I wanted to log my program's (the python program boots up a headless embedded device connected to a serial port) output to a text file console_log.txt using a shell script, for which, I wrote the following script:

current_time=$(date "+%Y.%m.%d-%H.%M.%S")
gnome-terminal -x bash -c "cd ~/Scripts;pwd;script -c \"python test.py $1\" ~/Output/console_log_$current_time.txt;exec $SHELL"

The above writes unreadable (non ASCII) characters to the file along the lines of:


Or gedit displays messages like:

Could not open the file “/home/user4/con…g_2017.04.06-16.05.48.txt”.
Unexpected error: Invalid byte sequence in conversion input

when I try to open the file.

When I run some thing simpler like:

script -c ifconfig ~/user4/Scripts/test.txt

it runs just fine, i.e, I get the expected output at the log file. How to avoid such unreadable characters?

  • 1
    what is the output of python -c "import sys;print(sys.getdefaultencoding())"' and of export | grep LANG ? Make sure both is set to UTF-8
    – Michael D.
    Apr 6 '17 at 11:19
  • I doubt this is the problem, but why are you using gnome-terminal? Why not just bash -c . . . ?
    – terdon
    Apr 6 '17 at 11:20
  • 1
    Do you get the same issue if you just direct the output of the python program to a file, or only with script? i.e. with python test.py > outfile instead of script -c "..." outfile ?
    – ilkkachu
    Apr 6 '17 at 11:31
  • 1
    Setting all your locales to en_IN.UTF-8. Your python file should be encoded to utf-8. add this line to the top of your python file # -*- coding: utf-8 -*- That should generate an utf-8 encoded textfile which will open.
    – Michael D.
    Apr 6 '17 at 12:46
  • 1
    @skrowten_hermit, I thought so, I don't think there's any reason for script to mangle the output, so it's probably not an issue with that. You might want to try to find out what part of the python program produces the output with broken encoding (and if it is actually broken)
    – ilkkachu
    Apr 6 '17 at 13:08

In case if you are running python script in Linux environment, follow the below steps before running the script:

$ script console_log.txt
$ run your python script
$ exit  (After the script completes execution)

Entire console logs will be captured in "console_log.txt"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.