当前位置:网站首页>(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] A library for invoking external command operations
- Multithreading (implementing multithreading, thread synchronization, producer and consumer)
- uniapp | 使用npm update更新后编译报错问题
- 4. PHP array and array sorting
- Praying: 1 vulnhub walkthrough
- hackmyvm-bunny walkthrough
- (6) 学生信息管理系统设计
- IO流、 编码表、 字符流、 字符缓冲流
- New usage of string variable parsing in PHP8.2
- PHP image compression to specified size
猜你喜欢
IP门禁:手把手教你用PHP实现一个IP防火墙
12. What is JS
(3) 字符串
阿里云设置域名解析重定向后,无法使用Chrome访问
hackmyvm: again walkthrough
uniapp | 使用npm update更新后编译报错问题
4. The form with the input
Solve the problem of Zlibrary stuck/can't find the domain name/reached the limit, the latest address of Zlibrary
解决 Zlibrary 卡死/找不到域名/达到限额问题,Zlibrary最新地址
Several interesting ways to open PHP: from basic to perverted
随机推荐
17.JS条件语句和循环,以及数据类型转换
ES6三点运算符、数组方法、字符串扩展方法
PHP 给图片添加全图水印
(3) Thinkphp6 database
17. JS conditional statements and loops, and data type conversion
SQL分类、DQL(数据查询语言)、以及相应SQL查询语句演示
SQL classification, DQL (Data Query Language), and corresponding SQL query statement demonstration
(3) 字符串
百度定位js API
[mikehaertl/php-shellcommand]一个用于调用外部命令操作的库
批量替换文件字体,简体->繁体
Thread Pool (Introduction and Use of Thread Pool)
4.PHP数组与数组排序
Warzone: 3 (Exogen) vulnhub walkthrough
hackmyvm-bunny walkthrough
PHP入门(自学笔记)
Solve the problem of uni - app packaged H5 website to download image
Several interesting ways to open PHP: from basic to perverted
ES6迭代器解释举例
TCP communications program