Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Exercise Example 99

Python Exercise Example 99

Python 100 Examples Python 100 Examples

**Problem:** There are two disk files A and B, each storing a line of letters. Merge the information in these two files (sorted in alphabetical order) and output it to a new file C.

**Analysis:** None.

Program source code:

#!/usr/bin/python

-- coding: UTF-8 --

if

name

==

'

main

'

:

import

string

fp

=

open

(

'

test1.txt

'

)

a

=

fp

.

read

(

)

fp

.

close

(

)

fp

=

open

(

'

test2.txt

'

)

b

=

fp

.

read

(

)

fp

.

close

(

)

fp

=

open

(

'

test3.txt

'

,

'

w

'

)

l

=

list

(

a

b

)

l

.

sort

(

)

s

=

'

'

s

=

s

.

join

(

l

)

fp

.

write

(

s

)

fp

.

close

(

)

Before running the above program, you need to create the files test1.txt and test2.txt in the directory where the script is executed.

After the above program runs successfully, open the test3.txt file and you can see the contents shown below:


123456

Python 100 Examples Python 100 Examples