Is there a more concise way of doing the following, e.g. on a single line and without the temporary variable f
?
with open('foo.txt', 'w') as f:
f.write('foobar')
I'm often writing a bunch of files in scripts and this looks ugly, e.g. in one of my test scripts I have the following ugly:
sorted_alembic_schema = sort_lines_without_commas(alembic_schema)
sorted_sqlalchemy_schema = sort_lines_without_commas(sqlalchemy_schema)
if sorted_alembic_schema != sorted_sqlalchemy_schema:
for file_name, file_contents in [('alembic.schema', alembic_schema),
('sqlalchemy.schema', sqlalchemy_schema),
('alembic.schema.sorted', sorted_alembic_schema),
('sqlalchemy.schema.sorted', sorted_sqlalchemy_schema)]:
with open(file_name, 'w') as f:
f.write(file_contents)
print("""
ERROR: alembic upgrade schema doesn't match sqlalchemy schema; see local files for more info, e.g.
meld sqlalchemy.schema alembic.schema
meld sqlalchemy.schema.sorted alembic.schema.sorted
FAIL"""
)