I very new to python. I am trying to find all python files that hav the string "DATASOURCE" inside them and to print the path name of the files. I'm using this code:
import os,glob
os.chdir(r"G:\PROJECTS\menofim_3_5\gis")
for file in glob.glob('*.py'):
with open(file) as f:
contents = f.read()
if 'DATASOURCE' in contents:
print file
I only get this result:
>>>
findAndReplaceWorkspacePaths-dwg.py
Remove FGB by dataSource excluding specific mxd.py
SelectLayerByLocation2.py
SelectLayerByLocation3.py
>>>
I have succeeded in printing the file name, but I don't know how to print the path name of the files.
Use os.path.join
to connect the dirname to the filename:
import os, glob
dirname = r"G:\PROJECTS\menofim_3_5\gis"
os.chdir(dirname)
for filename in glob.glob('*.py'):
with open(filename) as f:
contents = f.read()
if 'DATASOURCE' in contents:
print os.path.join(dirname, filename)
print file