Below you can find a simple script to search text in files. I'm looking for how to be more crossplatform i mean how to avoid '\' in path to look through all dirs and do it with standard library from Python. Because as i know, mac use '/' instead of backslash. Any idea how to do it?
#!/usr/bin/env python def find(text, search_path): res = [] for subdir, dirs, files in os.walk(search_path): for fname in files: if os.path.isfile(subdir + "\\" + fname): with open(subdir + "\\" + fname) as f: for i, line in enumerate(f): if text in line: res.append([fname, i])
os.path.joinos.path.joinis the correct solution, Windows is perfectly happy with/directory separators.PATHseparator (typically':'or';'), but the directory separator (typically'/'or'\').os.path.sep?", and the accepted answer from "Differences between use ofos.path.joinandos.sepconcatenation" for better ways to handle directory separators.