Embed bash in python

后端 未结 9 1309
别那么骄傲
别那么骄傲 2020-12-25 13:43

I am writting a Python script and I am running out of time. I need to do some things that I know pretty well in bash, so I just wonder how can I embed some bash lines into a

9条回答
  •  盖世英雄少女心
    2020-12-25 13:58

    Assuming the command is supported by the host system:

    import os
    os.system('command')
    

    If you have a long command, or a set of commands. you can use variables. eg:

    # this simple line will capture column five of file.log
    # and then removed blanklines, and gives output in filtered_content.txt.
    
    import os
    
    filter = "cat file.log | awk '{print $5}'| sed '/^$/d' > filtered_content.txt"
    
    os.system(filter)
    

提交回复
热议问题