File tree Expand file tree Collapse file tree 3 files changed +6
-2
lines changed
Expand file tree Collapse file tree 3 files changed +6
-2
lines changed Original file line number Diff line number Diff line change 11
2- # To read and write a file, you have to open a file handler, with the built-in
2+ # To read and write a file, you have to open a file handler using the built-in
33# `open()` function. Its only required argument is the path to the file.
44
55# We've used "f"-strings before to incorporate variable values, but now we
66# need to use an "r"-string: https://www.journaldev.com/23598/python-raw-string
77# They take backslashes literally — very important in Windows file/dir paths.
88
9+ # This may need to be altered for use on Windows
910filepath = r'07-files/shopping_list.csv'
1011
1112file = open (filepath )
1213
14+ # File objects (such as those returned by `open()`) are effectively a list:
1315for line in file :
14- print (line , end = "" )
16+ print (line , end = '' )
1517
1618file .close ()
Original file line number Diff line number Diff line change 33# script's Current Working Directory (CWD) is the root of the project.
44
55# We can access the CWD using the `getcwd()` function in the `os` module.
6+
67import os
78
89print (os .getcwd ())
Original file line number Diff line number Diff line change 1+
12# You can also use the `readlines()` method to iterate over the lines of a file
23
34filepath = r'07-files/shopping_list.csv'
You can’t perform that action at this time.
0 commit comments