Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
learn-python/src/files/test_file_reading.py at master · trekhleb/learn-python · GitHub
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
Events
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 }}
trekhleb
learn-python
Repository navigation
Code
Issues
17
(17)
Pull requests
26
(26)
Actions
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
learn-python
/
src
/
files
/
test_file_reading.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
77 lines (56 loc) · 3.16 KB
master
Breadcrumbs
learn-python
/
src
/
files
/
test_file_reading.py
Copy path
Top
File metadata and controls
Code
Blame
77 lines (56 loc) · 3.16 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Reading and Writing Files
@see: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files
The process of reading and writing to a file is like finding a book and opening a book.
First, the file is located, opened to the first page, then reading/writing begins until it reaches
the end of the file.
"""
def
test_files_open
():
"""Open files
open() returns a file object, and is most commonly used with two arguments:
open(filename, mode).
The first argument is a string containing the filename. The second argument is another string
containing a few characters describing the way in which the file will be used. mode can be:
- 'r' when the file will only be read,
- 'w' for only writing (an existing file with the same name will be erased),
- 'a' opens the file for appending; any data written to the file is automatically added to end.
- 'r+' opens the file for both reading and writing.
The mode argument is optional; 'r' will be assumed if it’s omitted.
Normally, files are opened in text mode, that means, you read and write strings from and to the
file, which are encoded in a specific encoding. If encoding is not specified, the default is
platform dependent (see open()). 'b' appended to the mode opens the file in binary mode: now
the data is read and written in the form of bytes objects. This mode should be used for all
files that don’t contain text.
In text mode, the default when reading is to convert platform-specific line endings (
\n
on
Unix,
\r
\n
on Windows) to just
\n
. When writing in text mode, the default is to convert
occurrences of
\n
back to platform-specific line endings. This behind-the-scenes modification
to file data is fine for text files, but will corrupt binary data like that in JPEG or EXE
files. Be very careful to use binary mode when reading and writing such files.
It is good practice to use the with keyword when dealing with file objects. The advantage is
that the file is properly closed after its suite finishes, even if an exception is raised at
some point. Using with is also much shorter than writing equivalent try-finally blocks:
"""
# Open files without using 'with' statement.
file
=
open
(
'src/files/multi_line_file.txt'
,
'r'
)
assert
not
file
.
closed
read_data
=
file
.
read
()
assert
read_data
==
(
'first line
\n
'
'second line
\n
'
'third line'
)
file
.
close
()
assert
file
.
closed
# Open file using with.
with
open
(
'src/files/multi_line_file.txt'
,
'r'
)
as
file
:
read_data
=
file
.
read
()
assert
read_data
==
(
'first line
\n
'
'second line
\n
'
'third line'
)
assert
file
.
closed
# If you’re not using the with keyword, then you should call f.close() to close the file and
# immediately free up any system resources used by it. If you don’t explicitly close a file,
# Python’s garbage collector will eventually destroy the object and close the open file for you,
# but the file may stay open for a while. Another risk is that different Python implementations
# will do this clean-up at different times.
You can’t perform that action at this time.