Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
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 }}
JasonDong97
learn-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
learn-python
/
src
/
files
/
test_file_methods.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
49 lines (37 loc) · 2.23 KB
master
Breadcrumbs
learn-python
/
src
/
files
/
test_file_methods.py
Copy path
Top
File metadata and controls
Code
Blame
49 lines (37 loc) · 2.23 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
39
40
41
42
43
44
45
46
47
48
49
"""文件对象的方法
@see: https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects
从文件中读取并不总是顺序的。
有一些方法可以查找文件中的特定位置,
就像翻到一本书的某一页一样。
"""
def
test_file_methods
():
"""文件对象的方法"""
multi_line_file
=
open
(
'src/files/multi_line_file.txt'
,
'r'
)
binary_file
=
open
(
'src/files/binary_file'
,
'r'
)
# 要读取文件的内容,调用f.read(size),它读取一定量的数据并将其作为字符串(文本模式)或字节对象(二进制模式)返回。
# size是一个可选的数字参数。
# 当size被省略或为负值时,将读取并返回文件的全部内容;
# 如果文件的大小是机器内存的两倍,那就是你的问题了。
# 否则,最多读取并返回size字节。
# 如果已经到达文件的末尾,f.read()将返回一个空字符串(").
read_data
=
multi_line_file
.
read
()
# pylint: disable=duplicate-code
assert
read_data
==
'first line
\n
second line
\n
third line'
# 要改变文件对象的位置,使用f.s seek(offset, from_what)。
# 位置从添加offset到参考点计算;参考点由from_what参数选择。
# from_what值为0表示从文件的开头开始,1使用当前文件位置,2使用文件的结尾作为参考点。
# From_what可以省略,默认值为0,使用文件的开头作为参考点。
assert
binary_file
.
seek
(
0
)
==
0
# 转到文件的第0个字节
assert
binary_file
.
seek
(
6
)
==
6
# 转到文件中的第6个字节
assert
binary_file
.
read
(
1
)
==
'6'
# readline()从文件中读取一行;
# 换行符(\n)将留在字符串的末尾,如果文件不以换行符结束,则只在文件的最后一行省略。
# 这使得返回值没有歧义;如果f.readline()返回一个空字符串,则表示已经到达文件的末尾,
# 而空行由'\n'表示,这是一个只包含一个换行符的字符串。
multi_line_file
.
seek
(
0
)
assert
multi_line_file
.
readline
()
==
'first line
\n
'
assert
multi_line_file
.
readline
()
==
'second line
\n
'
assert
multi_line_file
.
readline
()
==
'third line'
assert
multi_line_file
.
readline
()
==
''
multi_line_file
.
close
()
binary_file
.
close
()
You can’t perform that action at this time.