使用
os.walk。它递归地进入目录和子目录,并且已经为您提供了文件和目录的单独变量。
import reimport osfrom __future__ import with_statementPATH = "/home/tirengarfio/Desktop/pruebaba"for path, dirs, files in os.walk(PATH): for filename in files: fullpath = os.path.join(path, filename) with open(fullpath, 'r') as f: data = re.sub(r'(s*functions+.*s*{s*)', r'1echo "The function starts here."', f.read()) with open(fullpath, 'w') as f: f.write(data)


