指定多种文件类型,GLOB,Python 3.6

Specify Multiple File Types, GLOB, Python 3.6

我有大量的 html 和 css 文件(可能用于以后的类型)。

我已经使用 glob 编写了一个查找和替换脚本,它适用于 1 种文件类型..

我在迭代多个文件类型时遇到问题。

************更新***********

此代码完美运行!谢谢大家

files_extensions = ('.html', '.css')
find_str = 'http'
replace_str = 'https'

def find_and_replace(find, replace):
    for files in files_extensions:
        globby = (glob.glob('**/*' + files, recursive=True))
        print(globby)

        for file in globby:
            f = open(file, 'r', encoding='utf-8')
            file_data = f.read()
            f.close()

            new_data = file_data.replace(find, replace)

            f = open(file, 'w', encoding='utf-8')
            f.write(new_data)
            f.close()


find_and_replace(find_str, replace_str)

好吧,我会给你一个如何抓取多个类型的例子,我想你可以在你的代码中使用这个例子,但是如果你有什么困难,请提出来!

files_extension = ('*.html', '*.css')
all_files_with_extension = list()
for files in files_extension:
   all_files_with_extension.extend(glob.glob(files))

print all_files_with_extension