[英]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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.