โครงงาน python - silpakorn...

43
โครงงาน Python เรื่อง Function, Modules, Files I/O เสนอ อ.ดร.ทัศนวรรณ ศูนย์กลาง

Upload: others

Post on 27-Dec-2019

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

โครงงาน Pythonเรอง

Function, Modules, Files I/O

เสนออ.ดร.ทศนวรรณ ศนยกลาง

Page 2: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

FunctionFunction ใน python เราจะเรมตนดวยการใสค าวา def (ซงเปน format ของ python) ตามดวย name_function ,

“ ( ) “ และปดดวย colon “ : ”

Syntax :def name_function( parameters ) :

…return expression

def จะเปรยบเสมอน type function ของภาษาอน และ def สามารถเปนไดทก type

parameter ในภาษา python เราไมจ าเปนจะตองใส type ของ parameter function กสามารถเรยกใชได

Page 3: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Example 1:def printme( str ):

...

print str

return

def printme( a, b ):

...

return a+b

จะเหนไดวา parameter จะไมม type ของขอมล แตกสามารถใชได

Page 4: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Calling a Function (การเรยกใชฟงกชน)Example function form example 1:

def printme( str ):…print str;

return;# Now you can call printme functionprintme(“My name is Python");printme(“Ok , I know you");

Result :My name is Python

Ok , I know you

Page 5: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Pass by reference vs value (เรยกใชโดยการอางอง)Example 2:

def changeme( mylist ):

mylist.append([1,2,3,4]);

print "Values inside the function: ", mylist

return

# Now you can call changeme function

mylist = [10,20,30];

changeme( mylist );

print "Values outside the function: ", mylist

Result : Values inside the function: [10, 20, 30, [1, 2, 3, 4]]Values outside the function: [10, 20, 30, [1, 2, 3, 4]]

จะเหนวามการเรยก changeme นอกฟงกชนโดยสง mylist เปน parameter และฟงกชน changeme ท าการอางอง mylist จากนอกฟงกชนเพอมาเรยกใช

append เปนค าสงทใชในการตอทาย mylist

Page 6: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Result : [1, 2, 3, 4]

[10, 20, 30]

ตวอยางนกจะท าการอางองเชนเดยวกบ ตวอยางท 2

ฟงกชนท าการพมพคา mylist ทอยในฟงกชนเปนล าดบแรก แลวท าการพมพคา mylist นอกฟงกชนเปนล าดบถดไป

Example 3:def changeme( mylist ):

mylist = [1, 2, 3, 4];

print mylist

return

# Now you can call changeme function

mylist = [10,20,30];

changeme( mylist );

print mylist

Page 7: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Function Arguments(การสงพารามเตอร)Required argumentsExample 4 :

def printme( str ):…print str;

return;# Now you can call printme functionprintme( );

Result :มขอผดพลาดในการรน เนองการเรยกฟงกชนใชไมมตวสงผาน

(parameter)ไปยงฟงกชน

ม 4 รปแบบ คอ

1) Required arguments2) Keyword arguments3) Default arguments4) Variable-length arguments

Page 8: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Keyword argumentsExample 5 :

def printme( str ):…print str;

return;# Now you can call printme functionprintme( str = "My string");

Result :My string

เปนการสงตวหนงสอโดยพารามเตอร

Example 6 :def printinfo( name, age ):

…print "Name: ", name;print "Age ", age;

return;# Now you can call printinfo functionprintinfo( age=14, name=“python" );

Result :Name: mikiAge 50

เปนการสงตวหนงสอกบตวเลขโดยพารามเตอร

Page 9: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Default argumentsExample 7 :

def printinfo( name, age = 35 ):

print "Name: ", name;

print "Age ", age;

return;

# Now you can call printinfo function

printinfo( age=50, name=“python" );

printinfo( name=“GGWP" );

Result :Name: python

Age 50

Name: GGWP

Age 35

เมอท าการเรยกใชฟงกชน printinfo ทมพารามเตอรสงไปไมครบ ฟงกชนจะท าการน าคา Default ทเซตไวมาใสใหเอง

Page 10: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Variable-length argumentsSyntax :

def functionname([formal_args,] *var_args_tuple ):

"function_docstring"

function_suite

return expression

” * ” วางไวขางหนา arguments ทตองการก าหนด เปนVariable-length arguments

เปนฟงกชนทเราสามารถก าหนด arguments ตอนเรยกใชงานฟงกชนกตวกได

Example 8 :def printinfo( arg1, *vartuple ):

print "Output is: "

print arg1

for var in vartuple:

print var

return;

# Now you can call printinfo function

printinfo( 10 );

printinfo( 70, 60, 50 );

Result :Output is:

10

Output is:

70

60

50

Page 11: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

The Anonymous Functions(ฟงกชนทไมตองระบชอ)Syntax :

lambda [arg1 [,arg2,.....argn]]:expression

Example 9 :sum = lambda arg1, arg2: arg1 + arg2;

sum2 = lambda arg3, arg4: arg3 * arg4;

# Now you can call sum as a function

print “Summation : ” , sum( 10, 20 )

print “Multiple : ” , sum2( 20, 20 )

Result :Summation : 30

Multiple : 400

เปนการสรางฟงกชนแบบไมตองก าหนดชอ จะม format คอ lambda เสมอ

Page 12: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

The return Statement(สงคาออกจากฟงกชน)

Example 10 :def sum( arg1, arg2 ):

total = arg1 + arg2

print "Inside the function : ", total

return total;

# Now you can call sum function

total = sum( 10, 20 );

print "Outside the function : ", total

Result :Inside the function : 30

Outside the function : 30

return สามารถใชในการค านวน หรอ ลดการเขยนโคดทซ าๆ กนและมการเรยกใชกนไปมาระหวางฟงกชนดวย กนเอง หรอ เพอ ก าหนดรปแบบแสดงผลแบบตางๆ

Page 13: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Scope of Variables(ขอบเขตของการใชตวแปล)

ม 2 แบบ คอ ตวแปลแบบ Global และตวแปลแบบ Local

ตวแปล Global จะสามารถใชงานไดทกท สวนตวแปล Local จะใชงานไดแบบมขอบเขต ถาตวแปลนนอยในฟงกชน กจะสามารถใชไดแคในฟงกชนเทานน

Page 14: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

แบบฝกหด1) ท าการสรางฟงกชนแบบ Variable-length โดยใหพมพคา 1, 2, 3, 4, 5 ออกมา

หมายเหต ควรท าการสรางลปเพอใหสามารถพมพคาออกมาไดทกตว

Syntax :def functionname([formal_args,] *var_args_tuple ):

"function_docstring"

function_suite

return expression

2) จงสรางฟงกชนทไมตองการชอ (Anonymous Functions) มาหนงฟงกชน โดยก าหนดใหใชตวแปลทเปนตวเลข 2 ตว แลวท าการบวกคาในฟงกชน

Page 15: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

อางองhttp://www.learnpython.org/en/Functions

http://www.tutorialspoint.com/python/python_functions.htm

Page 16: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ModulesModule คอ กลมของ ตวแปร ฟงกชน หรอ คลาส ทอยในไฟลเดยวกน

โดยโมดลม 2 ประเภท ไดแก 1. โมดลทมอยแลวใน Library ของ Python เชน math , random , string , ..2. โมดลทผใชสรางขนเอง

Page 17: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

การเรยกใช Moduleจะตอง import โมดลเขามากอน ถงจะสามารถเรยกใชงานฟงกชน หรอคลาส

ทอยภายในโมดลได โดยมรปแบบ ดงน

import module1[, module2[,…moduleN]

การเรยกใช Function ในโมดลท import เขามา

module.function()

Page 18: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ตวอยาง การใชโมดลทมอยแลวใน Library ของ pythonimport mathprint(math.pi)

ผลลพธ

3.141592653589793

Page 19: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ตวอยาง การใชโมดลทผใชสรางขนเองสรางไฟลขนมาใหมชอ myModule.py แลวพมพโคดดานลาง

def Addition(num1,num2)return num1+num2

แลวท าการเรยกใชงานโมดล myModule จากอกไฟลimport myModuleprint(myModule.Addition(10,20))

ผลลพธ

30

Page 20: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

การใช from .. import ..เปนการเรยกใชบาง functioon จาก module ทท าการ import เขามา

โดยมรปแบบ ดงน

from module import function1[,… functionN][ *]

ตวอยาง การเรยกใชฟงกชน randint() (สมเลขจ านวนจรง) ทอยในโมดล randomซงมอยใน Library ของ Python

from random import randintprint(randint(1,10))

ผลลพธ(ตวเลขสมระหวาง 1-10)

Page 21: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ตวอยาง การเรยกใชทกๆฟงกชน ทอยในโมดล random ซงมอยใน Library ของPython โดยใช *

from random import *print(randint(1,10))

ผลลพธ

(ตวเลขสมระหวาง 1-10)

* ในการเรยกใชฟงกชนของ from .. import .. ไมตองท าการอางถงโมดลของฟงกชนนนๆกอน เชน random.randint() แตสามารถเรยกใชฟงกชน randint()ไดโดยตรง** การใช from .. import .. กบโมดลทผใชสรางขนมาเอง กสามารถท าไดในลกษณะเดยวกน

Page 22: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

dir() Functiondir() เปนฟงกชนทคนคาเปนชอทงหมดทถกก าหนดขนในโมดล ประกอบดวย

ชอโมดล , ชอตวแปล , ชอฟงกชนตวอยาง

import mathprint(dir(math))

ผลลพธ['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos',

'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

* จะตองท าการ import โมดลทตองการใชฟงกชน dir() กอน จงจะสามารถใชฟงกชน dir() ได

Page 23: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Namespaces and Scopingชอตวแปรและขอบเขตของตวแปร โดยตวแปรทอยในโปรแกรมหลกจะเรยกวา ตวแปร

ชนด Global สวนตวแปรทอยในฟงกชนจะเรยกวา ตวแปรชนด Localตวอยาง

year = 2014def NextYear():

year = year + 1print(year)NextYear()print(year)

ผลลพธUnboundLocalError: local variable 'year' referenced before assignment

* ผลลพธผดเนองจาก year ทอยนอกฟงกชน(ตวแปรชนด Global) กบ year ทอยในฟงกชน(ตวแปรชนด Local) ถอวาเปนตวแปรคนละตวกน แมชอจะเหมอนกนกตาม ซงมวธแก คอ ใสค าสง global ไวหนาตวแปร

Page 24: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ตวอยางyear = 2014def NextYear():

global yearyear = year + 1

print(year)NextYear()print(year)

ผลลพธ20142015

Page 25: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

แบบฝกหด1. สรางไฟลชอ myMath.py ดานในประกอบดวยฟงกชน โดยใหแตละฟงกชนรบ parameter เปน x และ y- Addition() คนคาเปน คา x บวกดวย คา y- Subtraction () คนคาเปน คา x ลบดวย คา y- Multiplication () คนคาเปน คา x คณดวย คา y- Division () คนคาเปน คา x หารดวย คา yแลวสรางอกไฟลชอ testMath.py แลวท าการเรยกใชแตละฟงกชนจาก myMath.py

ทมา : http://www.tutorialspoint.com/python/python_modules.htm

Page 26: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Files I/O

การรบ input ม 2 แบบ คอ รบคาจาก keyboard และ ไฟล txt สวนการแสดง output จะมรปแบบเดยว คอ การใชค าสง printมหวขอตางๆทเกยวของดงน

- รปแบบ input และ output- File Object Attributes- Reading and Writing Files- File Positions

Page 27: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

รปแบบ input และ output

1.การรบ input และ แสดง output แบบทวไปการรบ input จาก keyboard ม 2 รปแบบ คอ

1.1การใช raw_input

เปนการรบคาเขามาแลวคนคาเปนstring

syntax : <variable> = raw_input();

Page 28: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ตวอยาง

name = raw_input(“Enter your name : “);print “Your name is : “,nameinput = Robert

ผลลพธ

Enter your name : Robert

Your name is : Robert

Page 29: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

1.2การใช input

เปนการรบคาทเปนตวเลข

syntax : <variable> = input();

ตวอยางnumber = input(“Enter number : “);print “Number is : “,number*5input = 15

Page 30: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ผลลพธEnter number : 15Number is : 75

2.การรบ input จากไฟล txtจะใชประกอบไปดวย 2 Method คอ open กบ close2.1 open() Method

มหลกการท างานคอจะมตว pointer ชไปยงต าแหนงตางๆในไฟล ประกอบไปดวย 3 สวน ดงน

Page 31: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

1.file_name คอ ชอไฟล txt ทตองการชไปยงไฟลนน2.access_mode คอ โหมดของการเรยกใช ประกอบไปดวยโหมดหลกๆ ดงน

- r คอ โหมดส าหรบอานขอมลทอยในไฟลเทานน โดยตวชจะอยทต าแหนงแรกของไฟล และเปนคา default กรณทไมไดก าหนด access_mode

- w คอ โหมดส าหรบเขยนขอมลลงในไฟลเทานน โดยจะเปนการเขยนทบขอมลเดม มตวชอยทต าแหนงแรก ถาไฟลทเรยกไมมอย จะสรางไฟลใหมตามชอ file_name

Page 32: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

- a คอ โหมดส าหรบเพมขอมลลงไปในไฟล โดยเปนการเพมขอมลตอทายขอมลเดม มตวชอยทต าแหนงสดทายของไฟล ถาไฟลทเรยกไมมอย จะสรางไฟลใหมตามชอfile_name

- r+ คอ โหมดส าหรบอานและเขยนขอมล- a+ คอ โหมดส าหรบอานและเพมขอมล- w+ คอ โหมดส าหรบอานและเขยนขอมล3.buffering คอ การก าหนดการใช buffer โดยคา 0 คอ ไมใช buffer คา 1 คอ ใช buffer

Page 33: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

2.2 close() Method มการท างานคอถาเรยกใชจะเปนการปดการใชไฟลนน และถาใชงานไฟลอน ไฟลเดมจะถกปดโดยอตโนมต

syntax : filObject.close();

Page 34: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

File Object Attributes

เปนการเรยกใชเพอดคณสมบตตางๆของไฟลทเปดใชงาน มค าสงส าคญดงน1.file.closed – return คา true ถาไฟลนนถก close อย ถาไมใช จะ return คา false2.file.mode – return ชนดของ access_mode ของไฟลทเปดใชอย3.file.name – return ชอไฟล

ตวอยางfile = open(“test.txt”, ”a”)print "Name = ", test.nameprint "Status = ", test.closedprint "Mode = : ", test.mode

Page 35: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ผลลพธName = test.txtStatus = FalseMode = a

Page 36: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

Reading and Writing Files

1.write() Methodsyntax : fileObject.write(string);

ตวอยางfile = open(“test.txt”, “w”)file.write(“Python Java Banana”);file.close()

Page 37: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ผลลพธจะไดไฟลชอ test ทมขอมลอยคอ Python Java Banana

2.read() Methodsyntax : fileObject.read(count);

Page 38: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ตวอยางfile = open(“test.txt”, “r+”)str = test.read(9);print “Read String is : ”, strfile.close()

ผลลพธRead String is : Python Ja

Page 39: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

File Positions

ประกอบดวย method ทเกยวของกบต าแหนงของตว pointer ทชไปยงต าแหนงในไฟล ดงน

1.tell() Method - เปน method ทบอกต าแหนงปจจบนของตว pointer

2.seek(offset, from) Method – เปน method ทใชเปลยนต าแหนงตว pointer โดย

offset คอ จ านวนต าแหนงทจะขยบไปfrom คอ ต าแหนงจดเรมตนทจะท าการขยบ โดยให 0 = ต าแหนงเรมตนของไฟล 1 = ต าแหนงปจจบน2 = ต าแหนงสดทายของไฟล

Page 40: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ตวอยางfile = open("test.txt", "r+")str = test.read(9);print "Read String is : ", strposition = test.tell();print "Current file position : ", positionposition = test.seek(4, 1);str2 = test.read(4);print "Again Read String is : ", str2fo.close()

Page 41: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

ผลลพธ

Read String is : Python JaCurrent file position : 9Again Read String is : Bana

ขอความ : Python Java Banana

Page 42: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

แบบฝกหด

1.สรางไฟล txt ชอ newfile และท าการใสขอมลลงไป ดงน“Python Programming”

จงแสดงค าวา gram ออกมาทางจอภาพ

2.จงรบขอมลชอของนกศกษา และปพ.ศ.เกด เขามา จากนนแสดงชอ และอายของนกศกษา

ทมา : http://www.tutorialspoint.com/python/python_files_io.htm

Page 43: โครงงาน Python - Silpakorn Universitywebserv.cp.su.ac.th/lecturer/tasanawa/cs517321/python/... · 2014-11-30 · โครงงานPython ... ตัวอย่างน้ีก็จะทาการอ้างอิงเช่นเดียวกบั

รายชอผจดท า

นายธณชวฒน เจตนา 07550437 เรอง Functionนายธนพล สภาพ 07550439 เรอง Modules

นายธรศกด ทะเลทอง 07550446 เรอง Files I/O