Skip to content

Instantly share code, notes, and snippets.

@jaycody
Created November 13, 2017 07:16
Show Gist options
  • Save jaycody/dfc1d9f167f39a7e58801ec776f8a7de to your computer and use it in GitHub Desktop.
Save jaycody/dfc1d9f167f39a7e58801ec776f8a7de to your computer and use it in GitHub Desktop.
Open multiple files with multiple comma-separated open() statements in a single with
"""Python allows putting multiple open() statements in a single with.
comma-separate multiple open() statement
thank you steveha!
https://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement
answered Feb 14 '12 at 19:33
"""
def filter(txt, oldfile, newfile):
'''\
Read a list of names from a file line by line into an output file.
If a line begins with a particular name, insert a string of text
after the name before appending the line to the output file.
'''
with open(newfile, 'w') as outfile, open(oldfile, 'r', encoding='utf-8') as infile:
for line in infile:
if line.startswith(txt):
line = line[0:len(txt)] + ' - Truly a great person!\n'
outfile.write(line)
# input the name you want to check against
text = input('Please enter the name of a great person: ')
letsgo = filter(text,'Spanish', 'Spanish2')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment