Bài 6: Working with File in

Writing Files, File lesson9.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! /usr/bin/python
from sys import argv
script,filename=argv
print "Hello, I'm %s, I will open %s, truncate it and write some lines to it" % (script,filename)
print "First I will truncate it."
print "Opening the File"
file=open(filename,"w")
print "Truncate the File"
file.truncate()
print "Input 2 line to write to the file:"
line1=raw_input(">")
line2=raw_input(">")
print "Writing to file"
file.write(line1)
file.write("\n")
file.write(line2+"\nEnd")
print "Closing the file"
file.close()

Kết quả:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ python lesson9.py /tmp/txt
Hello, I'm lesson9.py, I will open /tmp/txt, truncate it and write some lines to it
First I will truncate it.
Opening the File
Truncate the File
Input 2 line to write to the file:
>Hello World
>I'm John
Writing to file
Closing the file
$ cat /tmp/txt
Hello World
I'm John
End

Copy Files, File lesson10.py

1
2
3
4
5
6
7
8
9
10
11
#! /usr/bin/python
from sys import argv
script,fromfile,tofile=argv
print "Copying from %s to %s" % (fromfile,tofile)
file1=open(fromfile)
input=file1.read()
print "Content of file1: %s" % input
print "Writing to %s ..." %tofile
file2=open(tofile,"w")
file2.write(input)
print "Done"

Kết quả:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ python lesson10.py /tmp/txt /tmp/txt2
Copying from /tmp/txt to /tmp/txt2
Content of file1: Hello World
I'm John
End
Writing to /tmp/txt2 ...
Done
$ cat /tmp/txt2
Hello World
I'm John
End
Bài 7: Function

Function and Args. File lesson11.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#! /usr/bin/python
def xin_chao(arg1,arg2):
    print "Xin chao %s,%s" % (arg1,arg2)
xin_chao("John","Tom")
print "============================="
def phep_nhan(*args):
    arg1,arg2=args
    kq=arg1*arg2
    print "Phep nhan:",kq
    
phep_nhan(6,3)
print "============================="
def phep_cong(toantu1,toantu2):
    print "Phep cong:",toantu1+toantu2
tmp1=10
tmp2=3
phep_cong(4,5)
phep_cong(tmp1,tmp2)
phep_cong(4+2,6+3)

Kết quả:

1
2
3
4
5
6
7
Xin chao John,Tom
=============================
Phep nhan: 18
=============================
Phep cong: 9
Phep cong: 13
Phep cong: 15

Function and file. File lesson12.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#! /usr/bin/python
file="/tmp/a"
fo=open(file)
def print_all(f):
    print f.read()
def rewind(f):
    f.seek(0)
    
def print_lines(line_count,f):
    print line_count,f.readline()
print_all(fo)
rewind(fo)
print_lines(1,fo)
print_lines(2,fo)
print_lines(3,fo)

Kết quả:

1
2
3
4
5
6
7
8
9
10
My name John
I'm a System Engineer
I'm Studying Python
End
1 My name John
2 I'm a System Engineer
3 I'm Studying Python

Lưu ý: nếu không rewind thì khi đọc sẽ không đọc lại từ đầu.

Bài 8: If…Else…

File lesson13.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#! /usr/bin/python
def so_sanh(a,b):
    if a==b:
      print "%s=%s" % (str(a),str(b))
    elif a>b:
      print "%s>%s" % (str(a),str(b))
    elif a<b:
      print "%s<%s" % (str(a),str(b))
print "Moi nhap so thu 1:"
s1=input(">")
print "Moi nhap so thu 2:"
s2=input(">")
so_sanh(s1,s2)

Kết quả:

1
2
3
4
5
Moi nhap so thu 1:
>4
Moi nhap so thu 2:
>6
4<6
Bài 9: For Loop

File lesson14.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#! /usr/bin/python
list = (1,2,3,4,5)
list_str = ["cat","dog","bat","lion"]
list_r = [1,"pens",2,"rullers",3,"balls"]
for count in list:
    print "count: %s" % count
print "================================="
for animal in list_str:
    print "We have a: %s" % animal
print "================================="
for lr in list_r:
    print "list r: %r" % lr
print "================================="
element=[]
for i in range(0,6):
    print "Adding %s to element" %i
    element.append(i)
for i in element:
    print "Let's get %s out from element" %i

Kết quả:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
count: 1
count: 2
count: 3
count: 4
count: 5
=================================
We have a: cat
We have a: dog
We have a: bat
We have a: lion
=================================
list r: 1
list r: 'pens'
list r: 2
list r: 'rullers'
list r: 3
list r: 'balls'
=================================
Adding 0 to element
Adding 1 to element
Adding 2 to element
Adding 3 to element
Adding 4 to element
Adding 5 to element
Let's get 0 out from element
Let's get 1 out from element
Let's get 2 out from element
Let's get 3 out from element
Let's get 4 out from element
Let's get 5 out from element
Bài 10: While Loop

File lesson15.py

1
2
3
4
5
6
7
8
#! /usr/bin/python
i=0
element=[]
while i<6:
    print "Adding %d to element" %i
    element.append(i)
    print "Element Now:", element
    i=i+1

Kết quả:

1
2
3
4
5
6
7
8
9
10
11
12
Adding 0 to element
Element Now: [0]
Adding 1 to element
Element Now: [0, 1]
Adding 2 to element
Element Now: [0, 1, 2]
Adding 3 to element
Element Now: [0, 1, 2, 3]
Adding 4 to element
Element Now: [0, 1, 2, 3, 4]
Adding 5 to element
Element Now: [0, 1, 2, 3, 4, 5]
Print Friendly

Comments

comments

Bài viết liên quan