0

I want to use Windows' command net statistics server and use extract date from it which is of pattern : (\d+-\d+-\d+ \d+:\d+:\d+)

def sysCmd(string):
try:
    res = subprocess.Popen(string)
    return res
except:
    return "NULL - Command Error"

loginTime = sysCmd ("net statistics server ") # windows command
loginRex = "(\d+-\d+-\d+ \d+:\d+:\d+)"
loginMatch = re.search(loginRex, str(loginTime))
print (loginMatch.group(0))

I get the error : AttributeError: 'NoneType' object has no attribute 'group'

The regular expression seem to work perfectly well in regex test harnesses for the output this windows command gives. Here it doesn't fail still after so much hit-and-trial.

What am I doing wrong?

p.s. I'm using Python 3

Aditya
  • 3,080
  • 24
  • 47
  • I think you have a typo after loginRex? Anyway, if you are getting the AttributeError it shouldn't be your problem, but just a typo you might want to fix. Can you print one of those loginTime so that we can see how they look like? There could be an answer there for those that do not use windows. Cheers! – Jblasco Jul 22 '13 at 13:37

2 Answers2

0

I am a user of python 2.7, so I don't know if there might be a difference in regular expressions there, but I don't know what the "-" means in them. For me, what works is:

loginRex = "(\d?\d?\d+:\d+:\d+)"

That will match 1981:05:17 and 81:05:17 both. If the latter is not necessary, just erase the "?". Can you try this?

Cheers!

Jblasco
  • 3,827
  • 22
  • 25
0

I realize the problem was actually there in the function where I called the subprocess. It had to be supplied with FLAGS.

res = subprocess.Popen(string)
return res

is now

res = subprocess.Popen(string, shell=False, stdout=subprocess.PIPE)
return str(res.communicate())

Damn silly mistake! ;)

Aditya
  • 3,080
  • 24
  • 47
  • Jeje, just not printing a test answer, the flags could have happened to anyone ;). Glad you solved it, anyway! – Jblasco Jul 22 '13 at 13:53