1、模块定义
用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质上就是.py结尾python文件。
分类:内置模块、开源模块、自定义模块。
2、导入模块
本质:导入模块的本质就是把python文件解释一遍;导入包的本质就是把包文件下面的init.py文件运行一遍。
1. 同目录下的模块导入
1 #同级目录间import 2 3 import module_name #直接导入模块 4 import module_name,module2_name #导入多个模块 使用:模块名.加函数名 5 from module_name import * #导入模块中所有函数和变量等。。不建议使用 6 from module_name import m1,m2,m3 #只导入模块中函数m1,m2,m3 使用:直接使用m1,m2,m3即可 7 from module_name import m1 as m #导入module_name模块中m1函数并且重新赋值给m 使用:直接输入m即可
2.不同目录下的模块导入
1 #不同目录之间import 当前文件main.py 2 3 #目录结构 4 # ├── Credit_card 5 # │ 6 # ├── core # 7 # │ ├── __init__.py 8 # │ └── main.py # 当前文件 9 # ├── conf # 10 # │ ├── __init__.py 11 # │ └── setting.py 12 # │ └── lzl.py 13 14 import sys,os 15 16 creditcard_path=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #当前目录的上上级目录绝对路径,即Creditcard目录 17 sys.path.insert(0,creditcard_path) #把Creditcard目录加入到系统路径列表第0个 18 19 print(sys.path) #打印系统环境路径 20 #['C:\\Users\\L\\PycharmProjects\\s14\\Day5\\Creditcard,.......] 21 22 #import settings.py #无法直接import 23 #ImportError: No module named 'settings' 24 25 from conf import settings #from目录import模块 26 27 settings.set() #执行settings下的函数 28 #in the settings
3.不同目录下模块连坏导入
目录结构 ├── Credit_card │ ├── core # │ ├── __init__.py │ └── main.py # 当前文件 ├── conf # │ ├── __init__.py │ └── setting.py │ └── lzl.py 目录结构
conf目录下的文件:
lzl.py
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #-Author-Lian 4 5 #当前文件lzl.py 6 def name(): 7 print("name is lzl") 8 9 lzl.py
setting.py
1 #当前文件settings,调用lzl.py模块 2 import lzl #导入模块lzl 3 4 def set(): 5 print("in the settings") 6 lzl.name() #运行lzl模块下的函数 7 8 set() #执行函数set 9 #in the settings 10 #name is lzl
此时执行settings.py文件没有任何问题,就是同一目录下的模块之间的导入,关键来了,此刻croe目录下的main.py导入模块settings会出现什么状况呢??!
core目录下的文件:
main.py
1 #不同目录之间连环import 当前文件main.py 2 import sys,os 3 4 creditcard_path=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #当前目录的上上级目录绝对路径,即Creditcard目录 5 sys.path.insert(0,creditcard_path) #把Creditcard目录加入到系统路径 6 7 from conf import settings 8 9 settings.set() #执行settings下的函数 10 # import lzl #导入模块lzl 11 #ImportError: No module named 'lzl'
可以看到直接报错:ImportError: No module named 'lzl',想想什么会报错类?!
刚才已经说到了,导入模块的本质就是把模块里的内容执行一遍,当main.py导入settings模块时,也会把settings里的内容执行一遍,即执行import lzl;
但是对于main.py来说,不能直接import lzl,所有就出现了刚才的报错,那有什么办法可以解决?!
对conf目录下settings.py文件进行修改
1 #当前文件settings,调用lzl.py模块 2 from . import lzl #通过相对路径导入模块lzl 3 4 def set(): 5 print("in the settings") 6 lzl.name() #运行lzl模块下的函数 7 8 set() #执行函数set 9 #in the settings 10 #name is lzl
此时执行main.py文件
1 #不同目录之间连环import 当前文件main.py 2 import sys,os 3 4 creditcard_path=os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #当前目录的上上级目录绝对路径,即Creditcard目录 5 sys.path.insert(0,creditcard_path) #把Creditcard目录加入到系统路径 6 7 from conf import settings 8 9 settings.set() #执行settings下的函数 10 # in the settings 11 # name is lzl
没有任何报错,我们只对settings修改了lzl模块的调用方式,结果就完全不同,此时的from . import lzl 用到的是相对路径,这就是相对路径的优点所在。
4、不同目录多个模块互相导入,用相对路径
目录结构:
Day5 ├── Credit_card ├── README.md ├── core │ ├── __init__.py │ └── main.py ├── conf │ ├── __init__.py │ └── setting.py │ └── lzl.py 目录结构
conf目录下的文件:
lzl.py
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #-Author-Lian 4 5 #当前文件lzl.py 相对路径 6 def name(): 7 print("name is lzl")
setting.py
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #-Author-Lian 4 5 #当前文件settings,调用lzl.py模块 相对路径 6 from . import lzl #通过相对路径导入模块lzl 7 8 def set(): 9 print("in the settings") 10 lzl.name() #运行lzl模块下的函数 11 12 set() #执行函数set 13 #in the settings 14 #name is lzl 15 16 settings
core目录下的文件:
main.py
1 #不同目录之间连环import 当前文件main.py 相对路径 2 3 from Day5.Credit_card.conf import settings 4 5 6 settings.set() #执行settings下的函数 7 # in the settings 8 # name is lzl
lzl.py以及settings.py文件未变,main.py文件去掉了繁杂的sys.path添加的过程,直接执行from Day5.Credit_card.conf import settings,使用相对路径,更加简洁方便!
二、内置模块
1、time和datatime模块
时间相关的操作,时间有三种表示方式:
-
- 时间戳 1970年1月1日之后的秒,即:time.time()
- 格式化的字符串 2014-11-11 11:11, 即:time.strftime('%Y-%m-%d')
- 结构化时间 元组包含了:年、日、星期等... time.struct_time 即:time.localtime()
1 Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:14:34) [MSC v.1900 32 bit (Intel)] on win32 2 Type "copyright", "credits" or "license()" for more information. 3 >>> 4 >>> import time 5 >>> 6 >>> print("time stamp:", time.time()) #时间戳 7 time stamp: 1525852970.2903516 8 9 >>> print("local time:", time.localtime()) # struct_time类型的本地时间 10 local time: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=16, tm_min=3, tm_sec=10, tm_wday=2, tm_yday=129, tm_isdst=0) 11 12 >>> print("utc time:", time.gmtime()) # struct_time类型的utc时间 13 utc time: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=8, tm_min=4, tm_sec=4, tm_wday=2, tm_yday=129, tm_isdst=0)
可以看出,当地时间(北京时间)比 UTC时间早8个小时。
各种时间形式的转换。
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 import time,calendar 6 7 # time模块中,三种时间形式之间的转换 8 time_stamp = time.time() # 时间戳 9 local_time = time.localtime(time_stamp)# 时间戳转struct_time类型的本地时间 10 utc_time = time.gmtime(time_stamp) # 时间戳转struct_time类型的utc时间 11 12 time_stamp_1 = time.mktime(local_time) # struct_time类型的本地时间转时间戳 13 time_stamp_2 = calendar.timegm(utc_time)# struct_time类型的utc时间转时间戳 14 15 print('time_stamp:',time_stamp) 16 print('local_time:',local_time) 17 print('utc_time:',utc_time) 18 print('time_stamp_local:',time_stamp_1) 19 print('time_stamp_utc:',time_stamp_2)
输出结果:
1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/time.py 2 time_stamp: 1525857093.0751617 3 local_time: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=17, tm_min=11, tm_sec=33, tm_wday=2, tm_yday=129, tm_isdst=0) 4 utc_time: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=9, tm_min=11, tm_sec=33, tm_wday=2, tm_yday=129, tm_isdst=0) 5 time_stamp_local: 1525857093.0 6 time_stamp_utc: 1525857093 7 8 Process finished with exit code 0
各种时间形式与字符串之间的转换。
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 import time 6 7 time_stamp = time.time() # 时间戳 8 local_time = time.localtime(time_stamp)# 时间戳转struct_time类型的本地时间 9 utc_time = time.gmtime(time_stamp) # 时间戳转struct_time类型的utc时间 10 11 # time模块中,三种时间形式和字符串之间的转换 12 print(time.ctime(time_stamp)) # 时间戳转字符串(本地时间字符串) 13 14 print(time.asctime(local_time)) # struct_time类型的本地时间转字符串 15 print(time.asctime(utc_time)) # struct_time类型的utc时间转字符串 16 17 print(time.strftime("%Y-%m-%d, %H:%M:%S, %w", local_time)) # struct_time类型的本地时间转字符串:自定义格式 18 print(time.strftime("%Y-%m-%d, %H:%M:%S, %w", utc_time)) # struct_time类型的utc时间转字符串:自定义格式 19 20 struct_time = time.strptime("2016-11-15, 15:32:12, 2", "%Y-%m-%d, %H:%M:%S, %w") # 字符串转struct_time类型 21 22 print(struct_time)
输出结果:
1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/time.py 2 Wed May 9 17:16:05 2018 3 Wed May 9 17:16:05 2018 4 Wed May 9 09:16:05 2018 5 2018-05-09, 17:16:05, 3 6 2018-05-09, 09:16:05, 3 7 time.struct_time(tm_year=2016, tm_mon=11, tm_mday=15, tm_hour=15, tm_min=32, tm_sec=12, tm_wday=1, tm_yday=320, tm_isdst=-1) 8 9 Process finished with exit code 0
结构化时间:
时间戳、格式化字符串、机构化时间相互转换:
datatime模块:
接下来再看datetime模块。该模块中包含4个主要的类:
-
-
- datetime.time:时间类,只包含时、分、秒、微秒等时间信息。
- datetime.date:日期类,只包含年、月、日、星期等日期信息。
- datetime.datetime:日期时间类,包含以上两者的全部信息。
- datetime.timedelta:时间日期差值类,用来表示两个datetime之间的差值。
-
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 import datetime 6 7 # datetime模块中datetime类的用法 8 a_datetime_local = datetime.datetime.now() # 获取datetime.datetime类型的本地时间 9 a_datetime_utc = datetime.datetime.utcnow() # 获取datetime.datetime类型的utc时间 10 11 print('local_time:',a_datetime_local) 12 print('utc_time:',a_datetime_utc) 13 print('format_local:',a_datetime_local.strftime("%Y-%m-%d, %H:%M:%S, %w")) # datetime.datetime类型转字符串 14 print('format_utc:',a_datetime_utc.strftime("%Y-%m-%d, %H:%M:%S, %w")) # datetime.datetime类型转字符串 15 16 a_datetime = datetime.datetime.strptime("2016-11-15, 15:32:12, 2", "%Y-%m-%d, %H:%M:%S, %w") # 字符串转datetime.datetime格式 17 18 print('a_datetime:',a_datetime)
输出结果:
1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/time.py 2 local_time: 2018-05-09 18:58:24.620881 3 utc_time: 2018-05-09 10:58:24.620881 4 format_local: 2018-05-09, 18:58:24, 3 5 format_utc: 2018-05-09, 10:58:24, 3 6 a_datetime: 2016-11-15 15:32:12 7 8 Process finished with exit code 0
datetime.datetime类和时间戳、struct_time类型之间的转换。
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 import time,datetime 6 7 # datetime模块中datetime类的用法 8 a_datetime_local = datetime.datetime.now() # 获取datetime.datetime类型的本地时间 9 a_datetime_utc = datetime.datetime.utcnow() # 获取datetime.datetime类型的utc时间 10 11 # datetime.datetime类和时间戳、struct_time类型之间的转换 12 time_stamp = a_datetime_local.timestamp() # datetime类型转时间戳 13 print('time_stamp:',time_stamp) 14 15 a_datetime_local = datetime.datetime.fromtimestamp(time.time()) # 时间戳转datetime.datetime类型的本地时间 16 a_datetime_utc = datetime.datetime.utcfromtimestamp(time.time()) # 时间戳转datetime.datetime类型的utc时间 17 print('a_datetime_local:',a_datetime_local) 18 print('a_datetime_utc:',a_datetime_utc) 19 20 print(a_datetime_local.timetuple()) # datetime类型转struct_time类型 21 print(a_datetime_utc.utctimetuple()) # datetime类型转struct_time类型
输出结果:
1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/time.py 2 time_stamp: 1525863948.894291 3 a_datetime_local: 2018-05-09 19:05:48.894292 4 a_datetime_utc: 2018-05-09 11:05:48.894292 5 time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=19, tm_min=5, tm_sec=48, tm_wday=2, tm_yday=129, tm_isdst=-1) 6 time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=11, tm_min=5, tm_sec=48, tm_wday=2, tm_yday=129, tm_isdst=0) 7 8 Process finished with exit code 0
2、random模块
生成随机数。
1 #random随机数模块 2 import random 3 4 print(random.random()) #生成0到1的随机数 5 #0.7308387398872364 6 7 print(random.randint(1,3)) #生成1-3随机数 8 #3 9 10 print(random.randrange(1,3)) #生成1-2随机数,不包含3 11 #2 12 13 print(random.choice("hello")) #随机选取字符串 14 #e 15 16 print(random.sample("hello",2)) #随机选取特定的字符 17 #['l', 'h'] 18 19 items = [1,2,3,4,5,6,7] 20 random.shuffle(items) 21 print(items) 22 #[2, 3, 1, 6, 4, 7, 5]
验证码。
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 import random 6 7 checkcode = '' 8 for i in range(4): 9 current = random.randrange(0, 4) 10 if current != i: 11 temp = chr(random.randint(65, 90)) #生成随机字母 12 else: 13 temp = random.randint(0, 9) 14 checkcode += str(temp) 15 16 print(checkcode)
输出结果:
1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/time.py 2 GNU1 3 4 Process finished with exit code 0
3、os模块
用于提供系统级别的操作。
1 #os模块 2 import os 3 4 os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径 5 os.chdir("dirname") #改变当前脚本工作目录;相当于shell下cd 6 os.curdir #返回当前目录: ('.') 7 os.pardir #获取当前目录的父目录字符串名:('..') 8 os.makedirs('dirname1/dirname2') #可生成多层递归目录 9 os.removedirs('dirname1') # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 10 os.mkdir('dirname') # 生成单级目录;相当于shell中mkdir dirname 11 os.rmdir('dirname') #删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname 12 os.listdir('dirname') #列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 13 os.remove() # 删除一个文件 14 os.rename("oldname","newname") # 重命名文件/目录 15 os.stat('path/filename') # 获取文件/目录信息 16 os.sep #输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/" 17 os.linesep #输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n" 18 os.pathsep #输出用于分割文件路径的字符串 19 os.name #输出字符串指示当前使用平台。win->'nt'; Linux->'posix' 20 os.system("bash command") #运行shell命令,直接显示 commans可以获取返回值 21 os.environ #获取系统环境变量 22 os.path.abspath(path) #返回path规范化的绝对路径 23 os.path.split(path) #将path分割成目录和文件名二元组返回 24 os.path.dirname(path) # 返回path的目录。其实就是os.path.split(path)的第一个元素 25 os.path.basename(path) # 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素 26 os.path.exists(path) #如果path存在,返回True;如果path不存在,返回False 27 os.path.isabs(path) #如果path是绝对路径,返回True 28 os.path.isfile(path) #如果path是一个存在的文件,返回True。否则返回False 29 os.path.isdir(path) #如果path是一个存在的目录,则返回True。否则返回False 30 os.path.join(path1[, path2[, ...]]) # 将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 31 os.path.getatime(path) #返回path所指向的文件或者目录的最后存取时间 32 os.path.getmtime(path) #返回path所指向的文件或者目录的最后修改时间
①os.dir、os.popen调用当前系统命令
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 import os 6 7 res = os.system('dir') #只执行dir命令,不保存结果 8 9 print(res) #res只返会执行命令的成功与否,成功为0 反之为1
执行结果:
1 E:\Python\PythonLearing\venv\Scripts\python.exe 2 ������ E �еľ��� �ĵ� 3 ������к��� 0000-5431 4 5 E:\Python\PythonLearing ��Ŀ¼ 6 7 2018/05/09 20:46 <DIR> . 8 2018/05/09 20:46 <DIR> .. 9 2018/05/09 20:45 <DIR> .idea 10 2018/04/01 01:32 378 changefile.py 11 2018/05/02 02:13 306 copy.py 12 2018/04/10 21:08 204 decorater.py 13 2018/04/01 00:28 722 File.py 14 2018/05/01 19:45 842 file_op.py 15 2018/05/05 22:58 1,253 func.py 16 2018/05/07 22:35 674 generater.py 17 2018/04/12 21:38 209 generator.py 18 2017/12/25 12:02 638 GuessAge.py 19 2018/03/16 00:47 95 HelloWorld.py 20 2017/12/25 02:38 671 Interaction.py 21 2018/05/09 02:51 73 module.py 22 2018/04/01 01:11 162 test.py 23 2018/05/09 20:46 122 time.py 24 2018/04/01 00:56 <DIR> venv 25 2018/05/01 19:42 649 yesterday 26 2018/03/31 23:49 643 yesterday.txt 27 2018/04/01 00:15 26 yesterday1.txt 28 2018/05/01 19:00 649 yesterday2 29 2018/04/01 01:33 639 yesterday_bak.txt 30 2018/05/02 02:13 <DIR> __pycache__ 31 2018/04/01 00:40 181 ������.py 32 20 ���ļ� 9,136 �ֽ� 33 5 ��Ŀ¼ 46,039,920,640 �����ֽ� 34 0
os.popen
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 import os 6 7 res = os.popen("dir") #不打印输出,保存执行结果 8 print(res) #打印res内存地址信息 9 10 res = os.popen("dir").read() #通过read去读取内存地址记录的信息 11 print(res)
执行结果:
1 E:\Python\PythonLearing\venv\Scripts\python.exe 2 <os._wrap_close object at 0x015DB3F0> 3 驱动器 E 中的卷是 文档 4 卷的序列号是 0000-5431 5 6 E:\Python\PythonLearing 的目录 7 8 2018/05/09 20:54 <DIR> . 9 2018/05/09 20:54 <DIR> .. 10 2018/05/09 20:52 <DIR> .idea 11 2018/04/01 01:32 378 changefile.py 12 2018/05/02 02:13 306 copy.py 13 2018/04/10 21:08 204 decorater.py 14 2018/04/01 00:28 722 File.py 15 2018/05/01 19:45 842 file_op.py 16 2018/05/05 22:58 1,253 func.py 17 2018/05/07 22:35 674 generater.py 18 2018/04/12 21:38 209 generator.py 19 2017/12/25 12:02 638 GuessAge.py 20 2018/03/16 00:47 95 HelloWorld.py 21 2017/12/25 02:38 671 Interaction.py 22 2018/05/09 02:51 73 module.py 23 2018/04/01 01:11 162 test.py 24 2018/05/09 20:54 325 time.py 25 2018/04/01 00:56 <DIR> venv 26 2018/05/01 19:42 649 yesterday 27 2018/03/31 23:49 643 yesterday.txt 28 2018/04/01 00:15 26 yesterday1.txt 29 2018/05/01 19:00 649 yesterday2 30 2018/04/01 01:33 639 yesterday_bak.txt 31 2018/05/02 02:13 <DIR> __pycache__ 32 2018/04/01 00:40 181 进度条.py 33 20 个文件 9,339 字节 34 5 个目录 46,039,887,872 可用字节
两者结合使用:
1 import os,sys 2 3 os.system(''.join(sys.argv[1:])) #把用户的输入的参数当作一条命令交给os.system来执行
4、sys模块
用于提供对解释器相关的操作。
1 #sys模块 2 import sys 3 4 sys.argv #命令行参数List,第一个元素是程序本身路径 5 sys.exit(n) #退出程序,正常退出时exit(0) 6 sys.version # 获取Python解释程序的版本信息 7 sys.maxint #最大的Int值 8 sys.path #返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 9 sys.platform #返回操作系统平台名称 10 sys.stdout.write('please:') 11 val = sys.stdin.readline()[:-1]
① 其中的 sys.argv 用来捕获执行执行python脚本时传入的参数:
1 import sys 2 3 strings = sys.argv 4 print(strings) # 所有参数 类型为列表 5 # ['start.py', 'hello', 'world'] 6 print(strings[0]) # 脚本名本身 7 # start.py 8 print(strings[1]) # 第一个参数 9 # hello 10 print(strings[2]) # 第二个参数 11 # world 12 print(strings[-1]) # 倒数第一个参数 13 # world 14 15 $ python test.py helo world 执行脚本
② sys.stdin信息输入:
1 #!/user/bin/env ptyhon 2 # -*- coding:utf-8 -*- 3 # Author: VisonWong 4 5 import sys 6 li = [] 7 file = sys.stdin 8 # <_io.TextIOWrapper name='<stdin>' mode='r' encoding='cp936'> file 9 for line in file: 10 li.append(line) 11 print(li) 12 print(line.strip()) # 这个很重要 strip去除末尾的换行符 本身print跟print直接回产生换行符 13 # 如果不进去去除末尾的换行符的话 会再多打印一行换行符
执行结果:
1 E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing/time.py 2 我 3 ['我\n'] 4 我 5 爱 6 ['我\n', '爱\n'] 7 爱 8 python 9 ['我\n', '爱\n', 'python\n'] 10 python 11 12 Process finished with exit code 1
③ sys.stdout 重定向输出
1 import sys 2 3 sys.stdout.write("asdgf") # 当我们在 Python 中打印对象调用 print obj 时候,事实上是调用了 sys.stdout.write(obj+'\n') 4 # asdgf
5、shutil模块
高级的 文件、文件夹、压缩包 处理模块。
① shutil.copyfileobj 将文件内容拷贝到另一个文件中,可以部分内容
1 #shutil 文件拷贝 2 import shutil 3 4 f1 = open("fsrc",encoding="utf-8") 5 6 f2 = open("fdst",encoding="utf-8") 7 8 shutil.copyfile(f1,f2) 9 10 #把文件f1里的内容拷贝到f2当中
函数源码
def copyfileobj(fsrc, fdst, length=16*1024): """copy data from file-like object fsrc to file-like object fdst""" while 1: buf = fsrc.read(length) if not buf: break fdst.write(buf) shutil.copyfileobj