当前位置:网站首页>(5) Modules and packages, encoding formats, file operations, directory operations
(5) Modules and packages, encoding formats, file operations, directory operations
2022-08-02 03:59:00 【stealth rookie】
目录
模块与包
模块
模块英文为Modules
在Python中一个扩展名为,py的文件就是一个模块
函数与模块的关系:一个模块中可以包含N多个函数
使用模块的好处:
1.方便其它程序和脚本的导入并使用
2.避免函数名和变量名冲突
3.提高代码的可维护性.
4.提高代码的可重用性

自定义模块
创建一个模块:新建一个.py文件,Try not to match the namepython自带的标准模块名称相同
导入模块:
import 模块名 [as 别名]
from 模块名称 import 函数/变量/类
以主程序形式运行
在每个模块的定义中都包括一个记录模块名称的变量_name_,程序可以检查该变量,以确定他们在哪个模块中执行.如果一个模块不是被导入到其它程序中执行,那么它可能在解释器的顶级模块中执行.顶级模块的_name_变量的值为__main_
if __name__ = '__main__' :
pass
#在cadd1.py文件里
def add(a,b):
return a+b
if __name__ == '__main__': #如果没有这条语句,caddwhen this module is imported,会执行10+20
print(add(10,20)) #只有点击运行csdd时,才会执行运算包
包是一个分层次的目录结构,它将一组功能相近的模块组织在一个目录
作用:
1.代码规范
2.避免模块名称冲突
包与目录的区别:
1.包含_init_,py文件的目录称为包
2.目录里通常不包含.init_.py文件
包的导入:
import 包名.模块名
Considerations when importing modules with packages:使用import方式进行导入时,只能跟包名或模块名
from pageage1 import module_A
from pageage1.module_A import a
使用from ...import可以导入包、模块、函数、变量

、
编码格式
常见的字符编码格式
python的解释器使用的是Unicode(内存)
.py文件在磁盘上使用UTF-8存储(外存)

文件操作
文件的读写原理
文件的读写操作俗称“IO操作”
The operating principle of file reading and writing:
解释器去运行.py文件的时候,It will call the resources on the operating system to operate the files on the hard disk (Read and write operations to files on disk)

文件读写流程:

文件的读写操作
内置函数open()创建文件对象

语法规则

file=open('a.txt','r')
print(file.readlines())
file.close()常用的文件打开模式
文件的类型,按照文件中数据的组织形式,文件分为两大类:
1.文本文件:存储的是普通“字符”文本,默认unicode字符集,可以使用记事本程序打开
2.二进制文件:把数据内容用“字节”进行存储,无法用记事本打开,必须用专用的软件打开,例如:mp3音频文件,jpg图片,.doc文档等

文件对象的常用方法

file=open('a.txt','r')
file.seek(2)
print(file.read())
file.close
#a.txtThe content is“中beautiful country”
#seek中的2,是两个字节,因为一个汉字占两个字节,So output from“国”开始
#结果输入,“beautiful country”with语句(上下文管理器)
with语句可以自动管理上下文资源,不论什么原因跳出with块,都能确保文件正确关闭,以此达到释放资源的目的

目录操作
1. os模块是Python内置的与操作系统功能和文件系统相关的模块.该模块中的语句的执行结果通常与操作系统有关,在不同的操作系统上运行,得到的结果可能不一样.
2. os模块与os.path模块用于对目录或文件进行操作
os模块操作目录相关函数

#osA module is a module related to the operating system
#演示
import os
os.system('notepad.exe')
os.system('calc.exe')
#直接调用可执行文件
os.startfile('C:\\......\\xx.exe')
print(os.getcwd())
lst=os.lisydir('路径')
os.mkdir('newdir2')os.path模块操作目录相关函数

os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上或者向下.
#示例
import os.path
print(os.path.abspath('demo13.py'))
#列出指定目录下的所有py文件
import os
path=os.getcwd()
lst=os.listdir(path)
for filename in lst:
if filename.endswitch('.py')
print(filename)边栏推荐
- [mikehaertl/php-shellcommand]一个用于调用外部命令操作的库
- (6) 学生信息管理系统设计
- (3) Thinkphp6 database
- 多线程(实现多线程、线程同步、生产者消费者)
- Xiaoyao multi-open emulator ADB driver connection
- [league/flysystem] An elegant and highly supported file operation interface
- PHP8.2中字符串变量解析的新用法
- 3. PHP data types, constants, strings and operators
- (1)Thinkphp6入门、安装视图、模板渲染、变量赋值
- PHP有哪些杀手级超厉害框架或库或应用?
猜你喜欢
随机推荐
PHP8.2 version release administrator and release plan
PHP入门(自学笔记)
[league/flysystem]一个优雅且支持度非常高的文件操作接口
(1) print()函数、转义字符、二进制与字符编码 、变量、数据类型、input()函数、运算符
ES6介绍+定义变量+不同情况下箭头函数的this指向
(1)Thinkphp6入门、安装视图、模板渲染、变量赋值
(3) Thinkphp6 database
使用PHPMailer发送邮件
数组的高级操作
What will be new in PHP8.2?
Advanced Operations on Arrays
13.JS输出内容和语法
When PHP initiates Alipay payment, the order information is garbled and solved
js eventLoop 事件循环机制
uniapp | 开发中遇到的兼容性问题(待续)
v-on基本使用、参数传递、修饰词
Thread Pool (Introduction and Use of Thread Pool)
4.PHP数组与数组排序
阿里云服务器如何使用admin账户登录
Batch replace file fonts, Simplified -> Traditional









