18
File I/O Th.S Trần Đức Lợi Pythonvietnam.info

Pbc day-09-fileio

Embed Size (px)

Citation preview

Page 1: Pbc day-09-fileio

File I/O

Th.S Trần Đức Lợi

Pythonvietnam.info

Page 2: Pbc day-09-fileio

Ôn tập bài cũ

• Ôn lại nội dung đã học về Function, module & package

• Chữa bài Quản lý thư viện

PythonBeginnerClass @loitd #pythonvietnam.info

Page 3: Pbc day-09-fileio

Mục đích bài học

• Tìm hiểu về file I/O trong python

PythonBeginnerClass @loitd #pythonvietnam.info

Page 4: Pbc day-09-fileio

I/O

• Raw_input()

• Input()

PythonBeginnerClass @loitd #pythonvietnam.info

Page 5: Pbc day-09-fileio

Mở một file

• Tác dụng của hàm open()

• Giá trị trả về

• Filehandle = open(filename, mode, buffer)

• Ví dụ:

– Open(‘file.loi’, ‘r’)

PythonBeginnerClass @loitd #pythonvietnam.info

Page 6: Pbc day-09-fileio

File I/O: Mode

• Mặc định là read.

• r: reading only, pointer at beginning. Error if not exists

• r+: both read & write

• w: writting only. Overwrite if existed.

• w+: both writting & reading. Ponter at the beginning.

• a: appending. pointer at the end if file exists. create new if not exists

• a+: appending & reading. pointer at the end.

• b: in binary mode

PythonBeginnerClass @loitd #pythonvietnam.info

Page 7: Pbc day-09-fileio

File I/O: Buffer

• nếu = 0, không sử dụng buffer

• nếu = 1: line buffering

• nếu > 1: buffer with buffer size

• nếu < 0: buffer size = system default

PythonBeginnerClass @loitd #pythonvietnam.info

Page 8: Pbc day-09-fileio

File I/O

• Trong trường hợp không có file?

• Try … except khi mở file

• File handle with for loop

– For l in handle:

• Bài tập:

– Mở một file do người dùng nhập vào

– Đếm số dòng trong một file đó?

PythonBeginnerClass @loitd #pythonvietnam.info

Page 9: Pbc day-09-fileio

File I/O: read & readline

• Handle.read()

• Handle.read(xbytes)

• Handle.readline()

• Handle.readline(xbytes)

• Handle.readlines(xbytes)

• EOF: trả về null

PythonBeginnerClass @loitd #pythonvietnam.info

Page 10: Pbc day-09-fileio

• Bài tập:

– In ra nội dung của 1 file sample với từng dòng

– Sử dụng các hàm xử lý chuỗi để đảm bảo in rađúng

PythonBeginnerClass @loitd #pythonvietnam.info

Page 11: Pbc day-09-fileio

File I/O: pointer

• Handle.write(‘x’)

• Handle.tell()

• Handle.seek(offset, from)

• From:

– 0: đầu file

– 1: vị trí hiện thời

– 2: cuối file

• Offset: bytes

PythonBeginnerClass @loitd #pythonvietnam.info

Page 12: Pbc day-09-fileio

OS module: file & folder

• Os.mkdir()

• Os.chdir()

• Os.getcwd()

• Os.rmdir()

• Os.remove()

• Os.path.isfile()

• Os.path.exists()

PythonBeginnerClass @loitd #pythonvietnam.info

Page 13: Pbc day-09-fileio

Zip files

• Zipfile module

• z = zipfile.ZipFile(filename)

• z.extractall(pwd='123456')

PythonBeginnerClass @loitd #pythonvietnam.info

Page 14: Pbc day-09-fileio

Zip file: Bài tập

• Xây dựng chương trình PythonZipReader vớicách sử dụng

• Usage: python zipreader.py filename keyfilename

PythonBeginnerClass @loitd #pythonvietnam.info

Page 15: Pbc day-09-fileio

File I/O: Bài tập

• Xây dựng chương trình PwdDictAtk

PythonBeginnerClass @loitd #pythonvietnam.info

Page 16: Pbc day-09-fileio

File I/O: Bài tập

• Viết chương trình đọc logfile và cung cấpthông tin các đoạn chat của vs một thành viênchỉ định.

PythonBeginnerClass @loitd #pythonvietnam.info

Page 17: Pbc day-09-fileio

File I/O: Bài tập

• Viết module configloader, có comment trongfile

PythonBeginnerClass @loitd #pythonvietnam.info

Page 18: Pbc day-09-fileio

Tổng kết bài học

• File open

• File read

• File write

• Zip file

• Folder/file in OS module

PythonBeginnerClass @loitd #pythonvietnam.info