Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help in pure Python code



  • Dear all,

     I have the list for all the computer names in my network (FIVE COMPUTERS) in a text file which name is "computers.txt".
    

    I have written the code below to pass all the computer names in the text file by using
    the first loop and execute the command "Win32_NetworkAdapter()" which will fetch certain
    information by using the second loop.

    The problem is that the code loop only locally in my pc not in the text file "computers". This is the output:

    The PC is mycomputername and the Speed is 1000000000
    The PC is mycomputername and the Speed is 1000000000
    The PC is mycomputername and the Speed is 1000000000
    The PC is mycomputername and the Speed is 1000000000
    The PC is mycomputername and the Speed is 1000000000

    *** I am using Python 3.7.**

    The code

    
    import wmi
    from multiprocessing import Process
    import  win32com.client
    def pcs():
        qbfile = open("computers.txt", "r")
        c = wmi.WMI ()
        for aline in qbfile:
            for s in c.Win32_NetworkAdapter():
                if s.Manufacturer == "Intel" and s.Speed == "1000000000":
                    print ("The PC is " +s.SystemName +" and the Speed is " +s.Speed )
            #qbfile.close()
    if __name__ == '__main__':
        Process(target=pcs).start()
    

    Thanks in advance.


  • Lifetime Qt Champion

    @monamour Your question is completely unrelated to Qt, but I will try to answer.
    You are using a relative path:

    qbfile = open("computers.txt", "r")
    

    Depending on current working directory it will not work as it will not find the file.
    Also this is not clear: "The problem is that the code loop only locally in my pc not in the text file "computers". What do you mean by that?



  • @jsulm Thanks for replying. Actually, I have searched for exact subject in this forum then I read it "General and Desktop". However, the question is not related to the QT, it is pure python.

    The directory is current directory.

    Many thanks,


  • Lifetime Qt Champion

    @monamour said in Help in pure Python code:

    The directory is current directory

    is that file inside this directory?



  • @monamour said in Help in pure Python code:

    for aline in qbfile:
        for s in c.Win32_NetworkAdapter():
    

    Your code does not even attempt to access the aline line returned out of the file. It simply loops through c.Win32_NetworkAdapter() each time there is a line in your file. That is why you are seeing the output you get. Whatever your code is supposed to do with the line aline you need to make it do it....



  • @jsulm Yes, inside it.



  • @JonB Many thanks I will try.



  • Dear All,

     It has been solved. The problem was the function **"strip()"** which remove the spaces between the characters in the text file "computers.txt".
    

    Many thanks,



  • @monamour
    In the code you asked about there is no call to any strip() function, and the code simply does not access the characters read from the file, whether you remove anything from them or not. So your solution is interesting.


Log in to reply