Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a3c8c4a

Browse files
authored
Create Exercise-21-Write-To-A-File.py
1 parent 1dc9816 commit a3c8c4a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎Exercise-21-Write-To-A-File.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
Exercise 21: Write To A File
3+
4+
Take the code from the How To Decode A Website exercise
5+
(if you didn’t do it or just want to play with some different
6+
code, use the code from the solution), and instead of
7+
printing the results to a screen, write the results to a txt
8+
file. In your code, just make up a name for the file you are
9+
saving to.
10+
11+
Extras:
12+
Ask the user to specify the name of the output file that will
13+
be saved.
14+
15+
'''
16+
17+
# Solution
18+
def main():
19+
file_name = input('Input file name:')
20+
21+
lines = ['Hello world\n', 'Hello world 2!\n', 'Hello world 3~\n']
22+
23+
with open(file_name, 'w') as file:
24+
for line in lines:
25+
file.write(line)
26+
27+
if __name__ == "__main__":
28+
main()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /