从多个文件中删除six.b [英] Removing six.b from multiple files

查看:60
本文介绍了从多个文件中删除six.b的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中有数十个文件,我想将 six.b("...")的所有出现更改为 b"..." .我可以使用某种正则表达式bash脚本来做到这一点吗?

I have dozens of files in the project and I want to change all occurences of six.b("...") to b"...". Can I do that with some sort of regex bash script?

推荐答案

完全可以在Python中实现,但是我首先要对项目树进行备份,然后:

It's possible entirely in Python, But I would first make a backup of my project tree, and then:


import re
import os

indir = 'files'

for root, dirs, files in os.walk(indir):
    for f in files:
        fname = os.path.join(root, f)
        with open(fname) as f:
            txt = f.read()
        txt = re.sub(r'six\.(b\("[^"]*"\))', r'\1', txt)
        with open(fname, 'w') as f:
            f.write(txt)
        print(fname)


这篇关于从多个文件中删除six.b的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆