Python file processing

Posted on 2011-10-28
Last Modified: 2012-08-13
I'm trying to get a script that loops through a list of servers, and does an iisreset.

Can someone help me out with this? I'm a little new, and I'm not sure why it fails. It's for windows. I had to use rstrip to get rid of \n. However, it seems to just say it's timing out, and not looping through the servers.


import wmi, getpass
import sys
import os
import re

uname = raw_input(r"Username With Domain (Example: prod\a-bob.rick): ")
passwd = getpass.getpass()

file = open(sys.argv[1], "r")
line = file.readline().rstrip()
x = line
status = raw_input(r"Reset Web Server ")
if str(status).lower() == 'y':
    while line:
            c = wmi.WMI(computer=x, user=uname, password=passwd)
            process_id, return_value = c.Win32_Process.Create(CommandLine="iisreset")
            for process in c.Win32_Process(ProcessId=process_id):
                 result = "Success"
        except wmi.x_access_denied:
            print("access denied")
            result = 'Denied'
        except wmi.x_wmi:
             result = 'Timeout'
            result = 'Fail'
        print(x, result)

Open in new window

Question by:winthrup8
    1 Comment
    LVL 16

    Accepted Solution

    There is an error in your while loop.

    you should remove line 11

    change line 15 to

    for line in file:
        line = line.rstrip()
        x = line

    I don't understand why you use the variable line and x if they store both the same value, but perhaps this is just because you simplified above example???

    Featured Post

    Hire Top Freelancers to Complete Python Projects

    Source the talented Expert Exchange community
    for top quality work on your Python projects.

    Hire the best. Collaborate easily. Get quality work.

    Join & Write a Comment

    Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
    Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now