0

I want to run multiple os process. The output of this process I route to file and every line in this file has wrong character

e.g.

for module in modules:
    module_path = os.path.join(git_dir, module.name)
    os.chdir(module_path)
    my_env = os.environ.copy()
    file_out = open("ouput.txt", "w")
    file_err = open("err.txt", "w")
    p = Popen(module.run_command,  env=my_env, stdout=file_out, stderr=file_err)

will produce string like that in output.txt

...
[0m[[0m[0minfo[0m] [0m[0mLoading global plugins from /home/myuser/.sbt/1.0/plugins[0m
...

instead of

...
[info] Loading global plugins from /home/myuser/.sbt/1.0/plugins
...

I can't manipulate the strings that ship to output text, because of its processing inside subprocess library or somewhere in os level.

Can anyone tell me how can I fix it?

theSemenov
  • 389
  • 4
  • 17
  • Possible duplicate of [How can I remove the ANSI escape sequences from a string in python](https://stackoverflow.com/questions/14693701/how-can-i-remove-the-ansi-escape-sequences-from-a-string-in-python) – e.dan May 01 '19 at 11:33
  • I don't think that upstairs post is the same. It shows how to fix string when you have control on it. In my case, I can't manipulate the resulting string. Output strings are processing inside subprocess library or somewhere in os level – theSemenov May 01 '19 at 12:02
  • 2
    @theSemenov, yes, you can. For example, you can use a file-like object instead of the output file, which would pre-process the string before writing it anywhere. – ForceBru May 01 '19 at 12:16

0 Answers0