How to run multi-line bash commands inside python?

后端 未结 2 741
悲哀的现实
悲哀的现实 2021-01-25 03:59

I want to run the following lines of linux bash commands inside a python program.

tail /var/log/omxlog | stdbuf -o0 grep player_new | while read i
d         


        
2条回答
  •  不思量自难忘°
    2021-01-25 04:26

    Here is a pure python solution that I think does the same as your bash:

    logname = '/var/log/omxlog'
    with open(logname, 'rb') as f:
        # not sure why you only want the last 10 lines, but here you go
        lines = f.readlines()[-10:]
    
    for line in lines:
        if 'player_new' in line:
            omxd = os.popen('omxd S').read()
            after_ = omxd[line.rfind('_')+1:]
            before_dot = after_[:after_.rfind('.')]
            print(before_dot)
    

提交回复
热议问题