我编写了以下脚本来匿名化 txt 文件中的电子邮件地址:
import io, os, sys
import re
def main():
try:
# Open the file.
myfile = open('emails.txt', 'r')
# Read the file's contents.
content = myfile.read()
content = re.sub(r'.+(?=@.+\.(com|edu))', "xxxx", content)
myfile = open('emails.txt', 'w')
myfile.write(content)
# Close the file.
myfile.close()
except IOError:
print('An error occured trying to read the file.')
except:
print('An error occured.')
main()
我想知道如何使目录及其子目录中的所有文件都可以使用。