Python Exercise Example 97
**Problem:** Input some characters from the keyboard and write them one by one to a disk file until a # is entered.
**Analysis:** None.
Program source code:
Example (Python 2.0+)
#!/usr/bin/python
-- coding: UTF-8 --
if
name
==
'
main
'
:
from
sys
import
stdout
filename
=
raw_input
(
'
Enter the file name:
\n
'
)
fp
=
open
(
filename
,
"
w
"
)
ch
=
raw_input
(
'
Enter a string:
\n
'
)
while
ch
!=
'
'
:
fp
.
write
(
ch
)
stdout
.
write
(
ch
)
ch
=
raw_input
(
'
'
)
fp
.
close
(
)
The output of the above example is:
Input filename:
runoobfile.txt
Input string:
runoob
runoob
google
google#
In the example, a file named runoobfile.txt is created and the two strings runoob and google are written to it. View the contents of runoobfile.txt:
$ cat runoobfile.txt
runoobgoogle
Python 100 Examples