Access file placed in multiple directories via python

80 Views Asked by At

I have two paths and in between one directory is variable. i want to access the xml file and make some changes with python script. how can i achieve this ?

p1  = '/opt/apache-tomcat-8.0.33/webapps/'
p2 = '/WEB-INF/classes/j2.xml'

full path looks like this :

/opt/apache-tomcat-8.0.33/webapps/text1/WEB-INF/classes/j2.xml
/opt/apache-tomcat-8.0.33/webapps/`abc2`/WEB-INF/classes/j2.xml

I have couple of files, placed in same manner. please help me on this. i tried to use blob i could not achieve the expected result.

2

There are 2 best solutions below

3
AudioBubble On BEST ANSWER
from glob import glob

p1  = '/opt/apache-tomcat-8.0.33/webapps/'
p2 = '/WEB-INF/classes/j2.xml'

print(glob(p1+'*'+p2))
0
Ajay2588 On

You can try this. Here list could be your dir contents.

>>> p1  = '/opt/apache-tomcat-8.0.33/webapps/'
>>> p2 = '/WEB-INF/classes/j2.xml'
>>> list = ['test','`some`']
>>> for i in list:
...     p1+i+p2
... 
'/opt/apache-tomcat-8.0.33/webapps/test/WEB-INF/classes/j2.xml'
'/opt/apache-tomcat-8.0.33/webapps/`some`/WEB-INF/classes/j2.xml'