python学习笔记-模块<11>

<h3>1. 什么是模块,就是一个.py文件</h3>
<h3>2. 导入模块</h3>
import module
import module as new_name
from module import objectname
from module import *
from module import objectname as newname
<pre>
import Tkinter #导入Tkinter 模块
import Tkinter as tk #将Tkinter模块导入进来并重命名为tk
form Tkinter import Label #导入模块中的方法,Label组件
from Tkinter import * #导入模块中的所有组件类和常量
From Tkinter import Label as Lb #将组件导入并重命名为Lb
</pre>

<h3>3. 导入调用</h3>
第一次导入时会出现.pyc的文件
当前文件的__name__属性为'__main__'
所以测试文件放在if条件下,所以只导入创建的对象

<h3>4. 包</h3>
1.包基本上就是另外一类模块。把用来处理一类事物的多个文件放在同一文件夹下组成的模块集。
2.要让python 将其作为包对待,包中每一层目录必须包含__init__.py的文件,为了将模块防止在包内,直接把模块放在包目录内即可。
3.可以通过一个main.py的主函数来将各个模块连起来
3.使用包可以避免多个文件重名的情况,不同的包下的文件同名不影响。
4.使用dir函数,它会将对象的所有特性(以及模块的所有函数,类,变量等)列出来。

<h3>5. os模块</h3>
<h5>5.1 环境变量函数</h5>
1.os.name 如果是windows操作系统返回’nt’,如果是其他系统则返回 ‘posix’
2.os.environ 返回系统的环境变量,以dict形式显示

<h5>5.2 文件操作函数</h5>
python os 模块包含普遍的操作系统功能常用方法:
os.getcwd() 返回当前工作目录
os.chdir(path) 改变工作目录
os.listdir(path=’.’) 列举指定目录中的文件名(‘.’表示当前目录,’..’代表上一级目录)
os.mkdir(path) 创建单层目录,如该目录已存在抛出异常
os.makedirs(path) 递归创建多层目录,如果该目录已经存在则抛出异常。
os.remove(path) 删除文件
os.rmdir(path) 删除单层目录,如该目录非空则抛出异常
os.removedirs(path) 递归删除目录,从子目录到父目录逐层尝试删除,遇到目录非空则抛出异常。
os.rename(old,new) 将文件old 重命名为new,文件和目录都使用这条命令
<pre>

import os
os.getcwd()
'C:\Python27'
os.chdir(r'D:\python\source')
os.getcwd()
'D:\python\source'
os.listdir('.')
['.idea',, 'AUTHORS', 'calc.py', 'ChangeLog', 'data.txt', 'databasse.py', 'errors.py', 'errors.pyc', 'fnord.tif', 'fonts_test.png', 'func.py', 'gen.py', 'guess.py', 'guessNumber.py', 'guessRock.py','test', 'test.html', 'test.py']
os.listdir('..')
[ 'python-2.7.12.msi','setuptools-28.3.0', 'setuptools-28.3.0.zip', 'source']
os.listdir(r'D:\python\source\test')

第一次导入时会出现.pyc的文件
当前文件的__name__属性为'__main__'
所以测试文件放在if条件下,所以只导入创建的对象

<h3>4. 包</h3>
1.包基本上就是另外一类模块。把用来处理一类事物的多个文件放在同一文件夹下组成的模块集。
2.要让python 将其作为包对待,包中每一层目录必须包含__init__.py的文件,为了将模块防止在包内,直接把模块放在包目录内即可。
3.可以通过一个main.py的主函数来将各个模块连起来
3.使用包可以避免多个文件重名的情况,不同的包下的文件同名不影响。
4.使用dir函数,它会将对象的所有特性(以及模块的所有函数,类,变量等)列出来。

<h3>5. os模块</h3>
<h5>5.1 环境变量函数</h5>
1.os.name 如果是windows操作系统返回’nt’,如果是其他系统则返回 ‘posix’
2.os.environ 返回系统的环境变量,以dict形式显示

<h5>5.2 文件操作函数</h5>
python os 模块包含普遍的操作系统功能常用方法:
os.getcwd() 返回当前工作目录
os.chdir(path) 改变工作目录
os.listdir(path=’.’) 列举指定目录中的文件名(‘.’表示当前目录,’..’代表上一级目录)
os.mkdir(path) 创建单层目录,如该目录已存在抛出异常
os.makedirs(path) 递归创建多层目录,如果该目录已经存在则抛出异常。
os.remove(path) 删除文件
os.rmdir(path) 删除单层目录,如该目录非空则抛出异常
os.removedirs(path) 递归删除目录,从子目录到父目录逐层尝试删除,遇到目录非空则抛出异常。
os.rename(old,new) 将文件old 重命名为new,文件和目录都使用这条命令
<pre>
>>> import os
>>> os.getcwd()
'C:\Python27'
>>> os.chdir(r'D:\python\source')
>>> os.getcwd()
'D:\python\source'
>>> os.listdir('.')
['.idea',, 'AUTHORS', 'calc.py', 'ChangeLog', 'data.txt', 'databasse.py', 'errors.py', 'errors.pyc', 'fnord.tif', 'fonts_test.png', 'func.py', 'gen.py', 'guess.py', 'guessNumber.py', 'guessRock.py','test', 'test.html', 'test.py']
>>> os.listdir('..')
[ 'python-2.7.12.msi','setuptools-28.3.0', 'setuptools-28.3.0.zip', 'source']
>>> os.listdir(r'D:\python\source\test')
['aa.jpg', 'aa.png', 'aaaa.py', 'addAgency.py', 'cccc.png', 'forelse.py', 'frame4.jpg', 'ma.py', 'init.py']
os.listdir(r'.\test')
['aa.jpg', 'aa.png', 'aaaa.py', 'addAgency.py', 'cccc.png', 'forelse.py', 'frame4.jpg', 'ma.py', 'init.py']
os.getcwd()
'D:\python\source'
os.mkdir('python')
os.mkdir('python')
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
os.mkdir('python')
WindowsError: [Error 183] : 'python'
os.makedirs(r'ppp\aaa')
os.remove('python')
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
os.remove('python')
WindowsError: [Error 5] : 'python'
os.remove(r'python\aaa.python')
os.rmdir('python')
os.removedirs(r'ppp\aaa')
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
os.removedirs(r'ppp\aaa')
File "C:\Python27\lib\os.py", line 170, in removedirs
rmdir(name)
WindowsError: [Error 145] : 'ppp\aaa'
os.remove(r'ppp\aaa\hh.txt')
os.removedirs(r'ppp\aaa')
os.mkdir('ajkfdhk')
os.rename('ajkfdhk.py','python')
</pre>

<h5>5.3 os.path 模块中关于路径常用的函数使用方法:</h5>
os.path.basename(path) 去掉目录路径,单独返回文件名
os.path.dirname(path) 去掉文件名,单独返回目录路径
os.path.join(path1,path2) 将path1,path2各部分组合成一个路径名
os.path.split(path) 分割文件名和路径,返回(f_path,f_name)元组。如果完全使用目录,它也会将最后一个目录作为文件名分离,且不会判断文件或者目录是否存在。
os.path.splitext(path)分离文件名与扩展名,返回(f_name,f_extension)元组
os.path.getsize(file)返回指定文件的尺寸,单位是字节
os.path.getatime(file) 返回指定文件最近的访问时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)
os.path.getctime(file) 返回指定文件创建时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)
os.path.getmtime(file) 返回指定文件最新的修改时间(浮点型秒数,可用time模块的gmtime()或localtime()函数换算)
<pre>

os.path.splitext('aaa.py')
('aaa', '.py')
os.path.basename(r'D:\python\source')
'source'
os.path.basename(r'D:\python\source\oo.py')
'oo.py'
os.path.dirname(r'D:\python\source\oo.py')
'D:\python\source'
os.path.join('python','test')
'python\test'
os.makedirs(r'python\test\aa.py')
os.path.split(r'python\test\aa.py')
('python\test', 'aa.py')
os.path.splitext(r'python\test\aa.py')
('python\test\aa', '.py')
os.path.getsize('test')
4096L
os.path.getatime('test')
1483093455.2816048
os.path.getctime('test')
1480577957.196684
os.path.getmtime('test')
1483093455.2816048
</pre>

<h5>5.4 返回True或False的函数os:</h5>
os.path.exists(path) 判断指定路径(目录或文件)是否存在
os.path.isabs(path) 判断指定路径是否为绝对路径
os.path.isdir(path) 判断指定路径是否存在且是一个目录
os.path.isfile(path) 判断指定路径是否存在且是一个文件
<pre>

os.path.exists('test')
True
os.path.isabs('test')
False
os.path.isabs(r'D:\python\source\python\test\aa.py')
True
os.path.isdir(r'D:\python\source\python\test\aa.py')
True
os.path.isfile(r'D:\python\source\python\test\aa.py')
False
os.path.isfile(r'D:\python\source\python\test\aa.py\ff.txt')
True
os.path.isdir('test')
True
os.path.isdir('ggg')
False
os.path.isfile('test')
False
os.path.isfile(r'.\test\ma.py')
True
</pre>

<h3>6. shutil模块:</h3>
<h5>6.1 复制文件</h5>
shutil.copyfile(‘oldfile’,’newfile’) oldfile和newfile都只能是文件
shutil.copy(‘oldfile’,’newfile’) oldfile只能是文件,newfile可以是文件也可以是目标目录
<h5>6.2 复制文件夹</h5>
shutil.copytree(‘olddir’,’newdir’),olddir和newdir都只能是目录,且newdir必须不存在
<h5>6.3 移动文件(目录)</h5>
shutil.move(‘oldname’,’newname’)
<h5>6.4 删除目录</h5>
os.rmdir(‘dir’)只能删除空目录
shutil.rmtree(‘dir’)空目录,有内容的目录都可以删
<pre>

import shutil
shutil.copyfile('oo.py','ooo.py')
shutil.copy('oo.py','python\ooo.py')
shutil.copytree('test','python2')
shutil.move('python2','python')
os.listdir(r'D:\python\source\python\test\aa.py')
['ff.txt']
os.remove(r'D:\python\source\python\test\aa.py\ff.txt')
shutil.rmtree(r'python')
</pre>

<h3>7. sys模块os</h3>
sys.argv: 用来向python解释器传递参数,名曰“命令行参数”
sys.exit(): 退出当前程序
sys.stdout: 与Python中的函数功能对照,sys.stdin获得输入,等价于python2中的raw_inpurt(),python3中的input(),sys.stdout负责输出
sys.path: 返回python目录下所有.pth路径文件下的内容集系统默认设置。可以通过列表的操作对其进行修改,不过这种更改只对当前的程序起作用。
<pre>

import sys
sys.path
['', 'C:\Python27\Lib\idlelib', 'C:\Windows\SYSTEM32\python27.zip', 'C:\Python27\DLLs', 'C:\Python27\lib', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27', 'C:\Python27\lib\site-packages']
sys.path.append(r'c:\pythonsource')
sys.path
['', 'C:\Python27\Lib\idlelib', 'C:\Windows\SYSTEM32\python27.zip', 'C:\Python27\DLLs', 'C:\Python27\lib', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27', 'C:\Python27\lib\site-packages', 'c:\\pythonsource']
</pre>


2.png

3.png

4.png

5.png

<h3>8. time模块:</h3>
时间获取和转换,时间元组(time.struct_time),gettime(),localtime()和striptime()以时间元组(struct_time)的形式返回
<pre>

import time
time.localtime()
time.struct_time(tm_year=2017, tm_mon=1, tm_mday=4, tm_hour=16, tm_min=59, tm_sec=39, tm_wday=2, tm_yday=4, tm_isdst=0)
</pre>
返回的元组的内容:


1.png

time.asctime([t]):接收时间元组并返回一个可读的形式。
<pre>
time.asctime(time.localtime())
'Wed Jan 04 17:02:10 2017'
</pre>
time.clock(): 用以浮点数计算的秒数返回当前的cpu时间,用来衡量不同程序的耗时
time.time(): 返回当前时间的时间戳,可以用来计算程序的耗时
时间戳(timestamp): 表示的是从1970年1月1日00:00:00开始按秒计算的偏移量(time.gmtime(0))
time.sleep(secs): 推迟调用线程的运行,secs的单位是秒
<pre>
import time
time.localtime()
time.struct_time(tm_year=2017, tm_mon=1, tm_mday=4, tm_hour=16, tm_min=59, tm_sec=39, tm_wday=2, tm_yday=4, tm_isdst=0)
t = time.localtime()
time.asctime(t)
'Wed Jan 04 17:00:29 2017'
time.asctime(time.localtime())
'Wed Jan 04 17:02:10 2017'
time.strftime('%Y-%m-%d %H:%M:%S')
'2017-01-04 17:02:38'
time.clock()
1.2411860277206487e-06
time.time()
1483520463.876
time.sleep(3)
</pre>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容