python -- 内置模块02
import os
os.makedirs('dirname1/dirname2') # 可生成多层递归目录
os.removedirs('dirname1') # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删 除,依此类推
os.mkdir('dirname') # 生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname') # 删除单级空目录,若⽬目录不不为空则无法删除,报错;相当于shell中 rmdir dirname
os.listdir('dirname') # 列列出指定目录下的所有文件和子目录,包括隐藏⽂文件,并以列列表⽅方式 打印
os.remove() # 删除一个文件
os.rename("oldname","newname") # 重命名文件/目录
os.stat('path/filename') # 获取文件/目录信息
os.system("bash command") # 运行行shell命令,直接显示
os.popen("bash command).read()") # 运行行shell命令,获取执行行结果
os.getcwd() # 获取当前工作目录,即当前python脚本工作的目录路路径
os.chdir("dirname") # 改变当前脚本工作目录;相当于shell下cd # os.path
os.path.abspath(path) # 返回path规范化的绝对路路径
os.path.split(path) # 将path分割成目录和文件名⼆二元组返回
os.path.dirname(path) # 返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path) # 返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path) # 如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path) # 如果path是绝对路径,返回True
os.path.isfile(path) # 如果path是⼀一个存在的文件,返回True。否则返回False
os.path.isdir(path) # 如果path是⼀一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]]) # 将多个路路径组合后返回,第⼀一个绝对路路径之前的参数 将被忽略
os.path.getatime(path) # 返回path所指向的文件或者目录的最后访问时间
os.path.getmtime(path) # 返回path所指向的文件或者⽬目录的最后修改时间
os.path.getsize(path) # 返回path的大小 # 特殊属性: os.sep 输出操作系统特定的路路径分隔符,win下为"\\",Linux下为"/"
os.linesep # 输出当前平台使用的行行终止符,win下为"\r\n",Linux下为"\n"
os.pathsep # 输出用于分割文件路径的字符串 win下为;,Linux下为:
os.name # 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
import os
os.makedirs("a/b/c") # 可以一次性创建多个目录
os.makedirs("a/d/f")
os.mkdir('a/b') # 上层文件夹必须存在
os.removedirs('a/b/c') # 可以帮我们删除当前这个目录级中所有空文件夹
os.rmdir('a/d/f') # 指定文件夹删除
os.system('dir') # 运行shell命令,获取执行结果(中文有可能会乱码,解决办法,运行下一个)
print(os.popen('dir').read()) # 执行shell脚本后者cmd命令
print(os.getcwd()) # 当前程序运行的文件夹
os.chdir('a') # 改变工作目录
print(os.getcwd())
stat 结构:
st_mode: inode 保护模式
st_ino: inode 节点号。
st_dev: inode 驻留的设备。
st_nlink: inode 的链接数。
st_uid: 所有者的用户ID。
st_gid: 所有者的组ID。
st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。
st_atime: 上次访问的时间。
st_mtime: 最后一次修改的时间。
st_ctime: 由操作系统报告的"ctime"。在某些系统上(如Unix)是最新的元数据更更改的时间,在 其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。
import sys
# print(sys.platform)
print(sys.path) # 找模块的, 必须要记住,模块的搜索路径
sys.path.append("F:\\python_workspace_hxt\\day21 继承")
import master
master.eat()
import sys
sys.argv # 命令行参数List,第一个元素是程序本身路径
sys.exit(n) # 退出程序,正常退出时exit(0),错误退出sys.exit(1)
sys.version # 获取Python解释程序的版本信息
sys.path # 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform # 返回操作系统平台名称
import pickle
class Elephant:
def __init__(self,name,weight,height):
self.name = name
self.weight = weight
self.height = height def play(self):
print(f"{self.name}大象特别喜欢吃香蕉") e = Elephant('懒懒','1023T','')
#
# e.play() # 序列化
ba = pickle.dumps(e) # 把对象进行序列化
print(ba) bs = b'\x80\x03c__main__\nElephant\nq\x00)\x81q\x01}q\x02(X\x04\x00\x00\x00nameq\x03X\x06\x00\x00\x00\xe6\x87\x92\xe6\x87\x92q\x04X\x06\x00\x00\x00weightq\x05X\x05\x00\x00\x001023Tq\x06X\x06\x00\x00\x00heightq\x07X\x03\x00\x00\x00176q\x08ub.'
# 反序列化
dx = pickle.loads(bs) # 反序列化,得到的是大象
dx.play()
e1 = Elephant('懒懒','1023T','')
e2 = Elephant('舟舟','789T','')
f = open('大象',mode='wb')
# 这也是序列化
pickle.dump(e1,f) # 没有s的这个方法是把对象打散写入到文件,序列化的内容不是给人看的,是给机器看的
pickle.dump(e2,f)
f = open('大象',mode='rb')
while 1:
try:
obj = pickle.load(f)
obj.play()
except Exception:
break
------2----------
e1 = Elephant('懒懒','1023T','')
e2 = Elephant('舟舟','789T','')
lst = [e1,e2]
pickle.dump(lst,open('大象',mode='wb'))
# 读
lst = pickle.load(open('大象',mode='rb'))
for dx in lst:
dx.play()
import json
dic = {'baby':None,'hb':False,'hut':'hutong'}
s = json.dumps(dic,ensure_ascii=False) # json 处理中文的问题
print(s) # {"baby": null, "hb": false, "hut": "hutong"}
# 读
d = json.loads('{"baby": null, "hb": false, "hut": "hutong"}')
print(d['baby'])
# 写
f = open('baby.json',mode='w',encoding='utf-8')
json.dump({'baby':None,'hb':False,'hut':'hutong'},f,ensure_ascii=False)
# 读
f = open('baby.json',mode='r',encoding='utf-8')
obj = json.load(f)
print(obj)
python -- 内置模块02的更多相关文章
- python内置模块[sys,os,os.path,stat]
python内置模块[sys,os,os.path,stat] 内置模块是python自带功能,在使用内置模块时,需要遵循 先导入在 使用 一.sys 对象 描述 sys.argv 命令行参数获取,返 ...
- Python学习02 列表 List
Python学习02 列表 List Python列表 List Python中的列表(List)用逗号分隔,方括号包围(comma-separated values (items) between ...
- python内置模块(4)
这一部分是python内置模块系列的最后一部分,介绍了一些小巧有用的内置模块. 目录: 1.random 2.shelve 3.getpass 4.zipfile 5.tarfile 6.bisect ...
- Python网络02 Python服务器进化
原文:Python网络02 Python服务器进化 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! **注意,在Python 3. ...
- Python学习笔记【第八篇】:Python内置模块
什么时模块 Python中的模块其实就是XXX.py 文件 模块分类 Python内置模块(标准库) 自定义模块 第三方模块 使用方法 import 模块名 form 模块名 import 方法名 说 ...
- Python内置模块与标准库
Python内置模块就是标准库(模块)吗?或者说Python的自带string模块是内置模块吗? 答案是:string不是内置模块,它是标准库.也就是说Python内置模块和标准库并不是同一种东西. ...
- python内置模块[re]
python内置模块[re] re模块: python的re模块(Regular Expression正则表达式)提供各种正则表达式的匹配操作,在文本解析.复杂字符串分析和信息提取时是一个非常有用的工 ...
- Python内置模块和第三方模块
1.Python内置模块和第三方模块 内置模块: Python中,安装好了Python后,本身就带有的库,就叫做Python的内置的库. 内置模块,也被称为Python的标准库. Python 2.x ...
- python进阶02 特殊方法与特殊属性
python进阶02 特殊方法与特殊属性 一.初始化.析构 1.初始化 # python中有很多双下划线开头且以下划线结尾的固定方法,它们会在特定的时机被触发执行,这便是特殊方法 # 在实例化的时候就 ...
随机推荐
- linux安装lamp/lamp/lanmp
wdcp安装lamp/lanp/lanmp 和宝塔(centOS)1. yum install -y wget //yum安装wegt2. wget http://dl.wdlinux.cn/fil ...
- python second lesson
1.系统模块 新建的文件名不能和导入的库名相同,要不然python会优先从自己的目录下寻找. import sys sys是一个系统变量,sys.argv会调出文件的相对路径,sys.argv[2] ...
- ts中的类的定义,继承和修饰符
自己搞一个ts文件 里面写代码如下,试一下就行了 /* 1.vscode配置自动编译 1.第一步 tsc --inti 生成tsconfig.json 改 "outDir": &q ...
- 正则表达式(re模块)
s='hello world' print(s.find('llo')) #找到llo ret=s.replace('ll','xx') #用xx代替ll print(ret) print(s.spl ...
- docker中i的作用
#docker container createKeep STDIN open even if not attached #docker container startAttach container ...
- matlab之中文字体乱码处理
- 国内老版本ubuntu更新源地址以及sources.list的配置方法
在终端输入并运行 sudo apt-get install vimsudo cp /etc/apt/sources.list /etc/apt/sources.list.backup (备份当前的源列 ...
- Eclipse Decompiler不生效解决办法
如下图,解决方案,Preferences->General->Editors->File Associations->*.class->Decompiler->De ...
- HTTPS通信原理
https的实现原理https用到了多种加密算法来实现通信安全,其中两种基本的加解密算法类型解释如下:(1)对称加密:密钥只有一个,加密解密为同一个密码,且加解密速度快,典型的对称加密算法有DES ...
- SPOJ - AMR11E
Arithmancy is Draco Malfoy's favorite subject, but what spoils it for him is that Hermione Granger i ...