当前位置:网站首页>读写文件,异常,模块和包
读写文件,异常,模块和包
2022-07-31 05:19:00 【m0_59138290】
读写文件:
写一个文本文件text:
with open("text.txt", "w", encoding="UTF-8") as f:
f.write("西安培华学院")
读取文本文件的内容
with open("text.txt", "r", encoding="UTF-8") as f:
result = f.read()
print(result)
复制这个文件text,拷贝为text2
with open("text.txt", "r", encoding="UTF-8") as f:
result = f.read()
with open("text2.txt", "w", encoding="UTF-8") as f:
f.write(result)
找一张图片,复制这个图片到copy.jpg
with open("1.png", "rb") as f:
result = f.read()
with open("copy.jpg", "wb") as f:
f.write(result)
异常
异常的完整格式,以及每个部分的意思
try:
#要执行的语句
except 异常 (as new_name):
处理异常的语句
......
except 异常 (as new_name): #except可以有多个处理异常的语句
else: #正常执行try中的语句,未报错
要执行的语句
finally:
最终都要执行的语句(有异常无异常都要执行
常遇见的>=5中异常的处理
try:
1 + "1"
except TypeError:
print('异常')
dict_data = {1: "1"}
try:
dict_data[2]
except KeyError:
print("异常")
list_data = [1, 2, 3]
try:
list_data.index(4)
except ValueError:
print("异常")
try:
list_data[5]
except IndexError:
print("异常")
try:
a = 50 - b
except NameError:
print("异常")
以及用异常处理来进行读写文件的文件关闭
try:
f = open('text.txt', 'r', encoding='utf-8')
print(f.read())
except FileNotFoundError:
print('无法打开指定的文件!')
except LookupError:
print('指定了未知的编码!')
except UnicodeDecodeError:
finally:
if f:
f.close()
如何主动抛出异常
try:
raise FError("自定义异常")
except FError as e:
print(e)
模块和包
什么叫模块?
模块是包含一组函数的文件,希望在应用程序中引用。
如何导入模块,如何导入模块中的具体内容
import modulename
from modulename import name1, name2
from modulename import name1 as newname
from modulename import * 调入所有非以下划线(_)开头的名称 以脚本的方式执行模块: python modulename.py arguments 模块的搜索路径: import sys print(sys.path)
import语句的实质是什么?
调用第三方库、函数或者导入包
什么是包
包是模块的一种形式,包的本质就是一个含有.py的文件的文件夹。
包和目录的区别
简单的说,python package就是一个目录,其中包括一组模块和一个_ _ init__.py文件。Image/_init _.pyjpg.pytiff.pybmp.py
只要image目录是我们程序目录的子目录,我们就可以导入image目录下的任意模块来为我们所用
- Directory:
Dictionary在pycharm中就是一个文件夹,放置资源文件等,该文件夹其中并不包含_ _ init.py_ _文件 - Python Package:
对于Python package 文件夹而言,与Dictionary不同之处在于其会自动创建_ _ init.py_ 文件。
简单的说,python package就是一个目录,其中包括一组模块和一个 _ init.py_ _文件。目录下具有init.py文件,这样可以通过from…import的方式进行.py文件的导入
__init__.py的作用
将文件夹变为一个Python模块
边栏推荐
猜你喜欢

Cholesterol-PEG-DBCO Cholesterol-Polyethylene Glycol-Diphenylcyclooctyne Chemical Reagent

Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl

Remote file xxx is mapped to the local path xxx and can‘t be found. You can continue debugging....

哈希表基础

Pytorch study notes 10 - detailed explanation of convolutional neural network and application of multi-classification task of mnist dataset

IDEA控制台不能输入信息的解决方法

Webrtc从理论到实践二: 架构

Rejection sampling note

The solution to the IDEA console not being able to enter information

Unity Text一个简单的输入特效
随机推荐
力扣刷题.快乐数
Pytorch study notes 13 - Basic_RNN
Qt TreeView 问题记录
【Rhapsody学习笔记】2:Count Down
Pytorch learning notes 09 - multiple classification problem
UR3机器人雅克比矩阵
解决nx安装 jtop问题
ES6-箭头函数
力扣.剑指offer05.替换空格
C语言知识点(二)
2021-09-30
Use usb_cam to open multiple cameras at the same time
Unity版本升级问题总结
Tensorflow steps on the pit while using it
ImportError: cannot import name 'Xxxx' from partially initialized module 'xx.xx.xx'
Remote file xxx is mapped to the local path xxx and can‘t be found. You can continue debugging....
nacos1.4.1创建配置报错
ES6-03-解构赋值
C语言数组的深度分析
Evaluating Machine Learning Models - Excerpt