WxRob87
WxRob87

Reputation: 55

Write Python output neatly in a txt file?

I am trying to create a program that lists and saves all running services on my Windows machine to a txt file. I got it to work but it is not listing line by line like in my output in the Python shell. Also, there are added parenthesis I do not want. See output vs txt file screenshot below. Also, my code is below.

Output vs txt file

My Code so far:

import win32con
import win32service

#Define ListServices class
def ListServices():
    resume = 0
    accessSCM = win32con.GENERIC_READ
    accessSrv = win32service.SC_MANAGER_ALL_ACCESS

    #Open Service Control Manager
    hscm = win32service.OpenSCManager(None, None, accessSCM)

    #Enumerate Service Control Manager DB
    typeFilter = win32service.SERVICE_WIN32
    stateFilter = win32service.SERVICE_ACTIVE

    statuses = win32service.EnumServicesStatus(hscm, typeFilter, stateFilter)

    for (short_name, desc, status) in statuses:
        #Save output to txt file
        f=open('MyServices.txt', 'w')
        f.write(str(statuses))
        f.close()
        #Print output and append 'Running' at the end of each line
        print(desc, status, '----------> Running') 

ListServices();

Upvotes: 1

Views: 768

Answers (4)

bunbun
bunbun

Reputation: 2676

You need to add a newline character at the end of your string, like this:

f.write(str(statuses)+"\n")

Upvotes: 0

Gohn67
Gohn67

Reputation: 10648

You have a few issues here.

First, you're overwriting your MyServices.text file each time in the loop. So you should open your file outside the loop.

Second, you are writing the statuses tuple in your code. Instead you should write out the individual line like you do in print

Third, f.write does not add a new line like print does. So you need to manually add a new line in your write function.

# Open MyServices.txt outside loop so you don't overwrite the file on each iteration
with open('MyServices.txt', 'w') as f:    
    for (short_name, desc, status) in statuses:
         # Write each status line individually and manually add a newline to the output.
         f.write("{}, {}, {}\n".format(short_name, desc, status))
         print(desc, status, '----------> Running') 

Upvotes: 0

WxRob87
WxRob87

Reputation: 55

Thanks for all your help guys!

This is what did the trick:

    with open('MyServices.txt', 'w') as f:
        for (short_name, desc, status) in statuses:
            f.write(str(desc))
            f.write(str(status))
            f.write('----------> Running')
            f.write(os.linesep)
            #Print output and append 'Running' at the end of each line
            print(desc, status, '----------> Running') 

Upvotes: 1

Mureinik
Mureinik

Reputation: 311938

write doesn't append a newline like print does, so you should take care of it yourself. Also, note that there's no reason to open and close the file on each iteration. Just leave it open as long as you need it:

with open('MyServices.txt', 'w') as f:
    for (short_name, desc, status) in statuses:
        f.write(str(statuses))
        f.write(os.linesep)
        #Print output and append 'Running' at the end of each line
        print(desc, status, '----------> Running') 

Upvotes: 1

Related Questions