WR7500
WR7500

Reputation: 449

Subprocess module feeding new line character

I'm writing a script that uses subprocess module to run the net time \\server windows command, and parse the output of that using regex.

import datetime
import subprocess
import re

myDate = datetime.datetime.now().strftime("%y-%m-%d")
myTime = datetime.datetime.now().strftime("%H:%M")
myDateTime = datetime.datetime.now().strftime("%y-%m-%d %H:%M")

results_file = 'C:\\temp\\ServerTimes_{0}.txt'.format(myDate)
servers_central = 'C:\\temp\\DMS-CENTRAL.txt'
sql_central = 'gt2-dms1cst'

def find_server_time(string):
    """
    Search for a time in HH:MM:SS format from a string. Needs re module.

    "string" is the string of text to be searched.
    """
    re1 = '.*?'  # Non-greedy match on filler
    re2 = '(?si)Local time.*?(\d{2}:\d{2}:\d{2}\s+[AP]M)'

    rg = re.compile(re1+re2, re.IGNORECASE | re.DOTALL)
    m = rg.search(string.decode())
    if m:
        time1 = m.group(1)
        print("("+time1+")"+"\n")

def capture_time(server):
    server_time_output = find_server_time(subprocess.check_output(r'net time \\{0}'.format(server)))
    return server_time_output

def time_diff(sql_time, server_time):
    """
    "sql_time" is the time reported by the SQL server. Different for each zone.
    "server_time" is the time reported by each individual server."
    """
    diff = sql_time - server_time
    diff_mins = diff / datetime.timedelta(minutes=1)
    return diff_mins

def main():
    STWrite = open(results_file, "w+")
    STWrite.write("Server Times - " + myDateTime + '\n')
    STWrite.write('\n')
    sql_cst = capture_time(sql_central)
    print(sql_cst)
    STWrite.write("SQL Server time is {0}. All comparisons will be based on this.\n".format(sql_cst))
    server_list = open(servers_central)
    for line in server_list:
        line_time = capture_time(line)
        line_diff = time_diff(sql_cst, line)
        STWrite.write("{0} time is {1}. Time Difference of {2} from SQL.\n".format(line, line_time, line_diff))


if __name__ == "__main__":
    main()

I seem to be running into an issue where Python is feeding in the new line character, \n, to the net time command, which is causing an error. The debugger shows:

(<class 'subprocess.CalledProcessError'>, CalledProcessError(1, 'net time \\\\gt2-dmcom1cst\n'), None)

How can I get this formatted correctly to feed into the subprocess call?

Upvotes: 2

Views: 1519

Answers (1)

stevieb
stevieb

Reputation: 9296

I'm suspecting the problem is in this line:

server_time_output = find_server_time(subprocess.check_output(r'net time \\{0}'.format(server)))

Append rstrip() to the variable, to strip off any whitespace at the end:

server_time_output = find_server_time(subprocess.check_output(r'net time \\{0}'.format(server.rstrip())))

Althouth server[:-1] will also work, if the server variable is ever input with no newline character, it'll strip off the last character regardless of what it is (ie. "servername" will become "servernam")

Upvotes: 2

Related Questions