当前位置:网站首页>(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)边栏推荐
- What are the killer super powerful frameworks or libraries or applications for PHP?
- JS objects, functions and scopes
- Shuriken: 1 vulnhub walkthrough
- [symfony/mailer]一个优雅易用的发送邮件类库
- [campo/random-user-agent]随机伪造你的User-Agent
- 微信小程序开发视频加载:[渲染层网络层错误] Failed to load media
- (1) print()函数、转义字符、二进制与字符编码 、变量、数据类型、input()函数、运算符
- The focus of the Dom implementation input triggers
- 第一次手撕代码,如何解出全排列问题
- 17. JS conditional statements and loops, and data type conversion
猜你喜欢

GreenOptic: 1 vulnhub walkthrough

PHP Foundation March Press Announcement Released

阿里云设置域名解析重定向后,无法使用Chrome访问

线程池(线程池介绍与使用)

Shuriken: 1 vulnhub walkthrough

IP access control: teach you how to implement an IP firewall with PHP

解决 Zlibrary 卡死/找不到域名/达到限额问题,Zlibrary最新地址

稳定好用的短连接生成平台,支持API批量生成

PHP入门(自学笔记)

TCP通信程序
随机推荐
MySql Advanced -- Constraints
[symfony/mailer] An elegant and easy-to-use mail library
4.PHP数组与数组排序
Stable and easy-to-use short connection generation platform, supporting API batch generation
AES加密的各种蛋疼方式方式
Phonebook
TypeScript error error TS2469, error TS2731 solution
阿里云设置域名解析重定向后,无法使用Chrome访问
多线程(实现多线程、线程同步、生产者消费者)
数组的高级操作
JS对象, 函数和作用域
ES6介绍+定义变量+不同情况下箭头函数的this指向
如何计算地球上两点的距离(附公式推导)
(2) Thinkphp6 template engine ** tag
hackmyvm-hopper walkthrough
MOMENTUM: 2 vulnhub walkthrough
Several interesting ways to open PHP: from basic to perverted
百度定位js API
微信小程序开发视频加载:[渲染层网络层错误] Failed to load media
Query the indexes of all tables in the database and parse them into sql