File Input/Output in Python Programming

 
M
o
d
u
l
e
 
4
 
 
P
a
r
t
 
7
 
File IO
 
Overview
 
Loading files to and from disk isn’t something
specifically related to Pygame
The game you are designing might require
reading from text files or writing to text files
We’ll go over the basics of File I/O (File
Input/Output) here
More will be covered in 1322
 
Reading from files
 
General syntax for reading from a text file
with open(“filename”, mode=“r”, encoding="utf-8") as f:
    pass
 
As long as you are inside the scope of the with
statement, you can read from the file
This is done by calling special functions from the f
object that the open() method creates
 
Reading from files – Reading all as a string
 
read() reads all lines inside the file and stores them
as a string
lines = “”
with open("text.txt", mode="r", encoding="utf-8") as f:
    lines = f.read()
 
text.txt
 
lines = 
“Alice\nBob\nCharlie”
 
f.read()
 
Reading from files – Reading all as a list
 
readlines() reads all lines inside the file and stores
them as a list of strings
lines = “”
with open("text.txt", mode="r", encoding="utf-8") as f:
    lines = f.readlines()
 
text.txt
 
lines = [
‘Alice\n’
,
  
‘Bob\n’
,
  
‘Charlie’
]
 
f.readlines()
 
Writing to files
 
General syntax for writing from a text file
with open(“filename”, 
mode=“w”
, encoding="utf-8") as f:
    pass
 
As long as you are inside the scope of the with
statement, you can write to the file
This is done by calling special functions from the f
object that the open() method creates
Notice that the mode has changed to “w”
 
Writing to files – String as an input
 
write() takes in a string and writes it to the file
Input must be a string
lines = “Alice\nBob\nCharlie”
with open("text.txt", mode=“w", encoding="utf-8") as f:
    f.write(lines)
 
text.txt
 
lines = 
‘Alice\nBob\nCharlie’
 
f.write()
 
Writing to files – List as an input
 
writelines() takes in a list of strings and writes it to
the file
Input must be a list of strings
lines = [‘Alice\n’, ‘Bob\n’, ‘Charlie’]
with open("text.txt", mode=“w", encoding="utf-8") as f:
    f.writelines(lines)
 
text.txt
 
lines = [
‘Alice\n’
,
  
‘Bob\n’
,
  
‘Charlie’
]
 
f.writelines()
 
Writing to files – Writing vs appending
 
There are two write modes available: writing and
appending
Writing: If the file already exists, 
erases its contents
and writes to the file
Appending: If the file already exists, writes at the
end of the file
Appending mode is 
mode
=
“a”
 
In both cases, if the file does not exist, it will be
created
 
Writing to files – Writing vs appending
 
lines = 
“David\nEdward\nFrank”
 
f.write(lines)
 
Write mode
 
f.write(lines)
 
Append mode
 
Writing to files – Uses for file i/o
 
Reading level information from disk into the game
Reading from or writing to a leaderboard
Allowing the user to save and load their game from
disk
 
Anything which requires permanence between
game sessions should be written to or read from
disk
 
Summary
 
Files can be written and read from disk
Information read from files can be returned either as a single
string or as a list of strings
Information written to files can be either a single string or as
a list of strings
When writing to a file, the contents of the file are first
erased, and only then are the new contents written
When appending to a file, the contents of the file are left
where they are, and the computer writes the new contents
after the old ones
 
 
 
Slide Note
Embed
Share

Exploring the basics of File I/O (Input/Output) in Python, including reading from and writing to text files. Learn how to read all content from a file as a string or a list, and how to write data to a file using Python's file handling capabilities.

  • Python
  • File I/O
  • Text Files
  • Reading
  • Writing

Uploaded on Oct 11, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Module 4 Module 4 Part 7 Part 7 File IO

  2. Overview Loading files to and from disk isn t something specifically related to Pygame The game you are designing might require reading from text files or writing to text files We ll go over the basics of File I/O (File Input/Output) here More will be covered in 1322

  3. Reading from files General syntax for reading from a text file with open( filename , mode= r , encoding="utf-8") as f: pass As long as you are inside the scope of the with statement, you can read from the file This is done by calling special functions from the f object that the open() method creates

  4. Reading from files Reading all as a string read() reads all lines inside the file and stores them as a string lines = with open("text.txt", mode="r", encoding="utf-8") as f: lines = f.read() text.txt f.read() lines = Alice\nBob\nCharlie

  5. Reading from files Reading all as a list readlines() reads all lines inside the file and stores them as a list of strings lines = with open("text.txt", mode="r", encoding="utf-8") as f: lines = f.readlines() text.txt f.readlines() lines = [ Alice\n , Bob\n , Charlie ]

  6. Writing to files General syntax for writing from a text file with open( filename , mode= w , encoding="utf-8") as f: pass As long as you are inside the scope of the with statement, you can write to the file This is done by calling special functions from the f object that the open() method creates Notice that the mode has changed to w

  7. Writing to files String as an input write() takes in a string and writes it to the file Input must be a string lines = Alice\nBob\nCharlie with open("text.txt", mode= w", encoding="utf-8") as f: f.write(lines) text.txt f.write() lines = Alice\nBob\nCharlie

  8. Writing to files List as an input writelines() takes in a list of strings and writes it to the file Input must be a list of strings lines = [ Alice\n , Bob\n , Charlie ] with open("text.txt", mode= w", encoding="utf-8") as f: f.writelines(lines) text.txt f.writelines() lines = [ Alice\n , Bob\n , Charlie ]

  9. Writing to files Writing vs appending There are two write modes available: writing and appending Writing: If the file already exists, erases its contents and writes to the file Appending: If the file already exists, writes at the end of the file Appending mode is mode= a In both cases, if the file does not exist, it will be created

  10. Writing to files Writing vs appending lines = David\nEdward\nFrank Write mode f.write(lines) Append mode f.write(lines)

  11. Writing to files Uses for file i/o Reading level information from disk into the game Reading from or writing to a leaderboard Allowing the user to save and load their game from disk Anything which requires permanence between game sessions should be written to or read from disk

  12. Summary Files can be written and read from disk Information read from files can be returned either as a single string or as a list of strings Information written to files can be either a single string or as a list of strings When writing to a file, the contents of the file are first erased, and only then are the new contents written When appending to a file, the contents of the file are left where they are, and the computer writes the new contents after the old ones

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#