[关闭]
@zhangyy 2019-12-11T10:17:14.000000Z 字数 5636 阅读 254

python 的文件操作与函数(三)

Python学习


一: Python的 文件操作
二: Python的 函数
三: Python函数的局部变量


1. Python的 文件操作

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. f = open("yesterday","r",encoding="utf-8") # 打开文件
  5. data = f.read()
  6. print(data)

image_1bii7qpn6ih31e9c1stq3qblq89.png-45kB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. f = open("yesterday1","w",encoding="utf-8") # 打开文件
  5. f.write("zhangyy")
  6. f.write("anebi")

image_1bii82ggl1us16tao9lrco23sm.png-42.6kB


  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. f = open("yesterday1","a",encoding="utf-8") # 打开文件
  5. f.write("homeupdate,")
  6. f.write("yangyang")
  7. print(f)

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. # f = open("yesterday","r",encoding="utf-8").read()
  5. f = open("yesterday","r",encoding="utf-8")
  6. for i in range(6):
  7. print(f.readline())

image_1bikqhq0a1grlbq21411hss155f9.png-76.6kB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. f = open("yesterday","r",encoding="utf-8")
  5. for index,line in enumerate(f.readlines()):
  6. if index == 9:
  7. print('----分割线-----')
  8. continue
  9. print(line.strip())

image_1bmbojr2b2441uflpss1e1p8bd9.png-82.2kB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. f = open("yesterday","r",encoding="utf-8")
  5. count = 0
  6. for line in f:
  7. if count == 9:
  8. print("----分割线-----")
  9. count +=1
  10. continue
  11. print(line)
  12. count +=1

image_1bmbpgjbs1iq4irt70g10t41dktm.png-78.9kB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. f = open('yesterday','r',encoding='utf-8')
  5. print(f.tell()) ### 定位标示符
  6. print(f.read(5))
  7. print(f.tell())
  8. print(f.seek(0)) ### 光标返回

image_1bmdokdle1ofmqef15ca6j71sej9.png-42.4kB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. import sys
  5. import time
  6. '''
  7. f = open("yesterday2","w",encoding="utf-8") # 打开文件
  8. f.write("hello1,\n")
  9. f.write("hello2,\n")
  10. '''
  11. for i in range(20):
  12. sys.stdout.write("#")
  13. sys.stdout.flush()
  14. time.sleep(0.1)
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. f = open("yesterday3","a",encoding="utf-8")
  5. f.seek(10)
  6. f.truncate(20)

image_1bmg9kd02d8j461r1d3v811l91j.png-17.1kB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. f = open("yesterday2","r+",encoding="utf-8") # 读写文件
  5. g = open("yesterday4",'w+',encoding="utf-8") # 写读文件
  6. t = open("yesterday4",'a+',encoding="utf-8") # 追加读写
  7. y = open("yesterday4",'rb') ## 以二进制的读写
  8. r = open("yesterday4",'wb') ### 以二进制的方式写读
  9. z = open("yesterday4",'wb') ### 以二进制的方式追加读写
  10. r.write("hello binary\n".encode())
  11. r.close()
  12. '''
  13. g.write("spark,\n")
  14. g.write("spark,\n")
  15. print(g.readline())
  16. print(g.readline())
  17. g.seek(5)
  18. g.write("------hadoop-------")
  19. print(g.readline())
  20. g.seek(5)
  21. '''

image_1bmgapioidol133n1t28502q0h20.png-21.4kB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. with open("yesterday","r",encoding="utf-8") as f:
  5. for line in f:
  6. print(line)
  7. # print(f.readline())

image_1bmgkjnb11dargasrrd1g6g7jl2d.png-66.1kB


二: Python的函数

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. ### 函数
  5. def func1():
  6. print('in thi func1')
  7. return 0
  8. # 过程 ---- 过程就是没用返回值的函数
  9. def func2():
  10. print("in the func2")
  11. x = func1()
  12. y = func2()
  13. print("from funcl return is %s" %x)
  14. print('from fun2 return is %s' %y)

image_1bmgsgenn1cpp1q1gn2s5r91nk79.png-34.2kB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. def test1():
  5. pass
  6. def test2():
  7. return 0
  8. def test3():
  9. return 0,'hello',['a','b','c'],{'name':'alex'}
  10. x=test1()
  11. y=test2()
  12. z=test3()
  13. print(x)
  14. print(y)
  15. print(z)
  16. ### python 的返回值最后 把所有的返回值 放到一个元组里面
  17. #### 函数的返回值:
  18. #### 默认没有返回值就是 none
  19. #### 返回值 = 1 默认的返回值 就是 一个object (对象)
  20. #### 返回值 > 1 默认返回值就是就是一个元组 (tuple)

image_1bmgum6v3s8v173u5tg1for1j4b16.png-37.9kB


  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. def test(x,y,z):
  5. print(x)
  6. print(y)
  7. print(z)
  8. # test(y=2,x=1) #与形参顺序无关
  9. # test(1,2) #与形参一一对应
  10. #test(x=2,3)
  11. test(3,z=2,y=6)

image_1bmh042gffp31h1h2mqut5122f1j.png-36.4kB

  1. def conn(host,port=3306):
  2. pass
  3. ### #默认参数特点:调用函数的时候,默认参数非必须传递
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. def test1(x,*args):
  5. print(x)
  6. print(args)
  7. test1(1,2,3,4,5,6,7)#!/usr/bin/env python
  8. # -*- coding: utf-8 -*-
  9. # Author:zhangyy
  10. #*args: 接收N 个 位置参数,转换成元组的方式
  11. #*kwargs:接收N 关键字参数,转换成字典的方式
  12. def test1(x,*args):
  13. print(x)
  14. print(args)
  15. test1(1,2,3,4,5,6,7)
  16. def test2(**kwargs):
  17. print(kwargs)
  18. print(kwargs['name']) ### 取值
  19. test2(name="zhangyy",age="8",sex = "boy")
  20. #test2(**{'name':'alex','age':8})
  21. def test3(name,**kwargs):
  22. print(name)
  23. print(kwargs)
  24. test3("alex",age =18,sex="boy")
  25. def test4(name,age =18,**kwargs):
  26. print(name)
  27. print(age)
  28. print(kwargs)
  29. test4("alex",sex="boy")
  30. def test5(name,age=18,*args,**kwargs):
  31. print(name)
  32. print(age)
  33. print(args)
  34. print(kwargs)
  35. test5('zhangyy',age=34,sex='boy',hobby='wtt')

image_1bmh3a31fhag1o058i1ut8nj8m.png-60.1kB


三:Python函数的局部变量

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. def change_name(name):
  5. print("before change",name)
  6. name = "flyfish" #### 局部变量问题 ,只在函数内生效。
  7. print("after change",name)
  8. name = "zhangyy"
  9. change_name(name)
  10. print(name)

image_1bmh3uj7lbmged2onb1kml1s5r1g.png-38kB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. school = "edu,oldboy"
  5. def change_name(name):
  6. global school #### 更改 局部变量的 提权问题
  7. school = "mage linux"
  8. print("before change",name,school)
  9. name = "flyfish"
  10. age = 23
  11. print("after change",name)
  12. name = "zhangyy"
  13. change_name(name)
  14. print(name)
  15. print(school)
  16. #### 不应该在函数里面改全局变量,特殊情况下才使用,一般不用

image_1bmiplq5414u51bh1hq385i1j0m9.png-48.9kB


  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. bigdata = ["spark","hadoop","hue","oozie"]
  5. def change_name():
  6. bigdata[0] = "zookeeper"
  7. print(bigdata)
  8. change_name()
  9. print(bigdata)
  10. #### 列表,集合, 字典 ,包括 类都可以修改

image_1bmiqe43a2gbnhs1q4a1t61radm.png-32.1kB

  1. 递归的特性
  2. 1. 必须有一个明确的结束条件
  3. 2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少
  4. 3. 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出)
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35]
  5. def binary_search(dataset, find_num):
  6. print(dataset)
  7. if len(dataset) > 1:
  8. mid = int(len(dataset) / 2)
  9. if dataset[mid] == find_num: # find it
  10. print("找到数字", dataset[mid])
  11. elif dataset[mid] > find_num: # 找的数在mid左面
  12. print("\033[31;1m找的数在mid[%s]左面\033[0m" % dataset[mid])
  13. return binary_search(dataset[0:mid], find_num)
  14. else: # 找的数在mid右面
  15. print("\033[32;1m找的数在mid[%s]右面\033[0m" % dataset[mid])
  16. return binary_search(dataset[mid + 1:], find_num)
  17. else:
  18. if dataset[0] == find_num: # find it
  19. print("找到数字啦", dataset[0])
  20. else:
  21. print("没的分了,要找的数字[%s]不在列表里" % find_num)
  22. binary_search(data, 66)

image_1bmirc66v13q4bm01ff918r81mne13.png-60.7kB


  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. def calc (n):
  5. print(n)
  6. if int (n/2) >0:
  7. return calc(int(n/2))
  8. print("-->",n)
  9. calc(10)

image_1bmirt1u812t71kn113s71k05bgg30.png-34kB

  1. 变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Author:zhangyy
  4. def add(x, y, f):
  5. return f(x) + f(y)
  6. res = add(3, -6, abs)
  7. print(res)

image_1bmisvif034slfiov11jm11e383d.png-30.2kB

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注