search

Home  >  Q&A  >  body text

linux - How to write the following shell command in python?

The original script was maintained by a senior. The shell script was too difficult to understand. The boss asked me to write it again in python. The function is as follows, which is to obtain the timestamp field in the svn log

]
I followed the python writing method, spliced ​​the commands together, and called popen to execute.

But an error was reported when running, as shown below:

Then I measured the front part of cmd, which is

The result is:

It can be seen that the result is correct, so the judgment problem occurs in the sed part. Please help me with your seniors.

漂亮男人漂亮男人2707 days ago1123

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-06-28 09:25:05

    Can it work if you copy the command directly to the command line? If it works, it means you made a mistake in organizing the string. For example, you missed the double quotes. For example, "%F" and '"%F"' in python are different, and '2' and '\2' are different. Different

    reply
    0
  • 怪我咯

    怪我咯2017-06-28 09:25:05

    When I first started working, I was too stuck to other people’s ideas. I thought about using Python to implement functions as much as possible. Just use python’s replacement function to solve the problem

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-06-28 09:25:05

    No need to sed/grep...

    # coding: utf8
    import re 
    from time import strftime, strptime
    from subprocess import Popen, PIPE
    
    cmd = 'svn log' # 不需要sed
    result = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
    stdout, stderr = result.communicate()
    
    for _ in re.findall(r'(?<=date>)([^<.]+)\.', stdout):
        print(strftime("%F %T", strptime(_, "%Y-%m-%dT%H:%M:%S")))
    

    reply
    0
  • Cancelreply