Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
ShakeCode
Python24
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Python24
/
00Python
/
day08
/
basic03.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
40 lines (35 loc) · 1.55 KB
master
Breadcrumbs
Python24
/
00Python
/
day08
/
basic03.py
Copy path
Top
File metadata and controls
Code
Blame
40 lines (35 loc) · 1.55 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
文件操作,open()返回一个file对象,r+主模式为读,但也可以写。 w+主模式为写,但也可以读
1- 如果"r"的方式打开,不存在会报错异常,存在的话会打开并且返回该对象
2- 如果"w"的方式open,不存在的文件不会报错,但是会**创建**一个新的文件
3- 读取和写入的时候不能write多次和read多次,只能open-close之后再次操作,否则无效
"""
# 文件的打开
a_file
=
open
(
"Test.txt"
,
"w+"
)
# 文件的读写
a_file
.
write
(
"Hello World!"
)
# 关闭文件
a_file
.
close
()
"""
1- read(2) 读取2个字节, 无参表示读取全部
2- readline() 读取一行,再次readline()就会光标读取下一行
"""
b_file
=
open
(
"Test.txt"
,
"r"
)
result
=
b_file
.
read
()
print
(
result
)
b_file
.
close
()
"""
中文的问题,可以在open的时候关键字参数指定编码open(encoding="utf-8")
file的访问模式:
1- "w" 先清空再写入,以文本的方式写入保存
2- "r" 不存在会直接异常报错
3- "a" -> append追加,也是写入的一种
4- "rb" 读取进制数据,显示的都是16进制的数字,需要解码decode
5- "wb" 以二进制的方式写入, 保存方式的不同,直接保存文本会报错,需要.encode("utf-8")保存
"""
c_file
=
open
(
"Test.txt"
,
"wb"
)
c_file
.
write
(
"中国"
.
encode
(
"utf-8"
))
# 文本.encode压缩
c_file
.
close
()
d_file
=
open
(
"Test.txt"
,
"rb"
)
# 中文只能read(3)显示一个汉字
result
=
d_file
.
read
().
decode
(
encoding
=
"utf-8"
)
# 二进制读取的解码
print
(
result
)
You can’t perform that action at this time.