Rewrite the program and Make sure for this program assignment when it is ran, it creates the
Question:
Rewrite the program and Make sure for this program assignment when it is ran, it creates the Ozymandias.txt output file.
File output
Instructions:
Some coding practice. This one is straightforward. To output to a file in Python, we use the following syntax:
with open('file_for_output.txt', 'w') as open_file:
open_file.write('blah blah test test')
where "file_for_output.txt" can be changed to whatever you want the output file to be.
For this assignment, the name of your output file should be "Ozymandias.txt"
Output the following poem using a separate "write" command for each line of the poem below. Be sure to test your code to make sure it works.
The poem to output:
Ozymandias by Percy Shelley
I met a traveller from an antique land
Who said: ‘Two vast and trunkless legs of stone
Stand in the desert. Near them, on the sand,
Half sunk, a shattered visage lies, whose frown,
And wrinkled lip, and sneer of cold command,
Tell that its sculptor well those passions read
Which yet survive, stamped on these lifeless things,
The hand that mocked them and the heart that fed.
And on the pedestal these words appear —
“My name is Ozymandias, king of kings:
Look on my works, ye Mighty, and despair!”
Expert Answer 1
default_image Vaseem Ahmad
Was the answer helpful ?
Sure, you can use Python to output the poem "Ozymandias" to a file named "Ozymandias.txt" using the with open syntax as you mentioned. Here's the code to do that:poem_lines = [
"Ozymandias by Percy Shelley",
"I met a traveller from an antique land",
"Who said: ‘Two vast and trunkless legs of stone",
"Stand in the desert. Near them, on the sand,",
"Half sunk, a shattered visage lies, whose frown,",
"And wrinkled lip, and sneer of cold command,",
"Tell that its sculptor well those passions read",
"Which yet survive, stamped on these lifeless things,",
"The hand that mocked them and the heart that fed.",
"And on the pedestal these words appear —",
"“My name is Ozymandias, king of kings:",
"Look on my works, ye Mighty, and despair"
]
with open('Ozymandias.txt', 'w') as open_file:
for line in poem_lines:
open_file.write(line + '')
This code will create a file named "Ozymandias.txt" and write each line of the poem as a separate line in the file. Make sure to save this code in a .py file and run it to generate the output default_image Sneha Chari
Was the answer helpful ?
Certainly! You can output the "Ozymandias" poem to a file named "Ozymandias.txt" .
Expert Answer 2
Python Code:
poem = """Ozymandias by Percy Shelley
I met a traveller from an antique land
Who said: ‘Two vast and trunkless legs of stone
Stand in the desert. Near them, on the sand,
Half sunk, a shattered visage lies, whose frown,
And wrinkled lip, and sneer of cold command,
Tell that its sculptor well those passions read
Which yet survive, stamped on these lifeless things,
The hand that mocked them and the heart that fed.
And on the pedestal these words appear —
“My name is Ozymandias, king of kings:
Look on my works, ye Mighty, and despair!”"""
with open('Ozymandias.txt', 'w') as open_file:
for line in poem.split(''):
open_file.write(line + '')
The above Code will Create a file names as "Ozymandias.txt" and write each sentence of poem as a seperate line.
note: save this code in a .py file and run it to generate the output file.
Financial Accounting and Reporting a Global Perspective
ISBN: 978-1408076866
4th edition
Authors: Michel Lebas, Herve Stolowy, Yuan Ding