当前位置:网站首页>(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)边栏推荐
- PHP Foundation March Press Announcement Released
- 17. JS conditional statements and loops, and data type conversion
- About the apache .htaccess file of tp
- Several interesting ways to open PHP: from basic to perverted
- 2.PHP变量、输出、EOF、条件语句
- (8) requests、os、sys、re、_thread
- Solve the problem of Zlibrary stuck/can't find the domain name/reached the limit, the latest address of Zlibrary
- [sebastian/diff]一个比较两段文本的历史变化扩展库
- 4. PHP array and array sorting
- 轮播图详解(完整代码在最后)
猜你喜欢
随机推荐
(3) 字符串
Shuriken: 1 vulnhub walkthrough
The Error in the render: "TypeError: always read the properties of null '0' (reading)" Error solution
hackmyvm: again walkthrough
(2) Thinkphp6 template engine ** tag
百度定位js API
(8) requests, os, sys, re, _thread
线程池(线程池介绍与使用)
easyswoole uses redis to perform geoRadiusByMember Count invalid fix
3. PHP data types, constants, strings and operators
Scrapy爬虫遇见重定向301/302问题解决方法
Kali install IDEA
QR code generation API interface, which can be directly connected as an A tag
js 中this指向
轮播图详解(完整代码在最后)
PHP 发起支付宝支付时 订单信息乱码解决
[campo/random-user-agent] Randomly fake your User-Agent
v-on基本使用、参数传递、修饰词
PHP有哪些框架?
hackmyvm: controller walkthrough









