Using glob in Python

Asked

Viewed 620 times

2

I am mounting a client that will play the found files in a folder for a SOAP Webservice

This Webservice has two methods that we will call MET1 and MET2.

  • When found files end with . XML and start with LCL, then I run MET1
  • When found files end with . XML and start with CRC, then I run MET2

So far I’ve done it this way:

from zeep import Client
from xml.dom import minidom
import os
import glob


hasha = "9f56ccba6d88d2b089ab8a9fb40dd46f" 


for file in glob.glob('M:\SARA.VEMBU\TRACKING\SINERGIA\\retorno\\*.XML'):
    for file in glob.glob('M:\SARA.VEMBU\TRACKING\SINERGIA\\retorno\\LCL*'):
         arquivo = minidom.parse(file)
         arquivo = arquivo.toxml()
         arquivo = arquivo[22:]  

         client = Client('http://teste.php?wsdl')
         result = client.service.met1(arquivo, hasha)

         print(result.met1Response)

    for file in glob.glob('M:\SARA.VEMBU\TRACKING\SINERGIA\\retorno\\CRC*'):
         arquivo = minidom.parse(file)
         arquivo = arquivo.toxml()
         arquivo = arquivo[22:]  

         client = Client('http://teste.php?wsdl')
         result = client.service.met2(arquivo, hasha)

         print(result.met2Response)

However the code does not return anything, even the folder containing files with the suffix and prefix indicated

BS.: I’m a beginner in Python

  • First, why did you make one glob inside the other? The first, who searches for XML files, has no need to be there. Second: what is your question? Is the code giving any error? What is the message of this error? If it is not giving error, is it producing some unexpected output? What would she be and what would be expected?

  • @Andersoncarloswoss on the glob: It is because two filters are needed

  • @Andersoncarloswoss On the way out: I edited the question

  • You can join the filters in just one: glob.glob(r'M:\\...\\LCL*.xml'). This way you list all the files started with LCL and XML extension. Also try to escape all the backslashes from the path.

  • @Andersoncarloswoss what good is this r before the road?

  • To define that the string is raw (raw string). That is, if there is " n", "r" or any similar character they will not escape. Possibly using the "r" at first, nor need to escape the inverted bars as I said. I’m not sure. You even tested?

  • @Andersoncarloswoss I removed the r and made the escape. If you want can answer the question

Show 2 more comments

1 answer

0

All you have to do is make a if to see if the file starts with one or the other beginning.

from zeep import Client
from xml.dom import minidom
import os
import glob

hasha = "9f56ccba6d88d2b089ab8a9fb40dd46f" 

for file in glob.glob('M:\SARA.VEMBU\TRACKING\SINERGIA\\retorno\\*.XML'):
    arquivo = minidom.parse(file)
    arquivo = arquivo.toxml()
    arquivo = arquivo[22:]  

    client = Client('http://teste.php?wsdl')

    filename = os.path.filename(file)

    if filename.startswith('LLC'):
        result = client.service.met1(arquivo, hasha)
        print(result.met1Response)
    else if filename.startswith('CRC'):
        result = client.service.met2(arquivo, hasha)
        print(result.met2Response) 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.