.txt
, .py
,
.cpp
.jpg
, .gif
,
.png
, .exe
open()
functionMode | Definition |
---|---|
w |
writes data, if the file already exists then the data will be lost |
r |
reads data, the cursor inside the file is positioned at the beginning |
w+ |
writes and reads data, previous data in the file will be lost |
r+ |
reads and writes data, previous data in the file will not be deleted, and the cursor inside the file is positioned at the beginning |
a+ |
appends and reads data, the file cursor is positioned at the end of the file |
x |
creates the specified file, or returns an error if the file exists |
t
- for text mode (the default option)b
- for binary modeclose()
functionread()
: will read all the lines and return
them line-by-lineread(n)
: will read \(n\) bytes from the filereadlines()
: will return all strings as
elements in a listTraceback (most recent call last):
File "source.py", line 1, in <module>
exampleFile = open("myfile.txt")
FileNotFoundError: [Errno 2] No such file or directory: 'myfile.txt'
seek()
function
open()
function and one of the following modes:
a
for append mode, which will append
contents to the end of the filew
for write mode, which will overwrite
existing contentwith
Keywordwith
keyword
close()
functionos
moduleChecking the Working/Active Directory
getcwd()
functionmkdir()
function/
”) in the string
will create a subdirectory
my_directory
”)rmdir()
function
rename()
functionisfile()
functionremove()
functionwalk()
functionos
module also contains a
system()
function which is useful to run shell
commands from within Pythoncsv
is an abbreviation for
Comma-Separated
Values.csv
,
”) to separate the values
within a file, hence the name
;
”)csv
file can be
something such as:name,age,course
Ian,33,Computer Science
Terry,Unknown,Computer Science
name, age, course
are the labels for each
column of data,
”) separating each value for the
columnscsv
file
as a table, for example:name | age | course |
---|---|---|
Ian | 33 | Computer Science |
Terry | Unknown | Computer Science |
csv
csv
module enables you to read the
contents of a fileDictReader
DictReader
from the csv
modulecsv
Exampleimport csv
with open("data.csv", "r") as csvFile:
csvReader = csv.reader(csvFile, delimiter=",")
for row in csvReader:
print("\n\n", row)
DictReader
Example iimport csv
with open("data.csv", "r") as csvFile:
csvReader = csv.DictReader(csvFile)
for row in csvReader:
print("\n\n", row)
DictReader
Example iiimport csv
names = []
with open("data.csv", "r") as csvFile:
csvReader = csv.DictReader(csvFile)
for row in csvReader:
names.append(row["name"])
print("\n\n", names)
csv
DictWriter
csv
csv
module also enables users to write
to a CSV file
DictWriter
DictWriter
classcsv
filewriterow()
csv
Exampleimport csv
with open("data.csv", "a", newline='\n') as csvFile:
csvWriter = csv.writer(csvFile, delimiter=",")
csvWriter.writerow(['Daniel', 'Unknown', 'Ethical Hacking and Cyber Security'])
name,age,course
Ian,33,Computer Science
Terry,Unknown,Computer Science
Daniel,Unknown,Ethical Hacking and Cyber Security
DictWriter
Exampleimport csv
aStudent = {"name": "Daniel",
"age": "Unknown",
"course": "Ethical Hacking and Cyber Security"}
with open("data.csv", "a", newline='\n') as csvFile:
csvWriter = csv.DictWriter(csvFile, fieldnames=aStudent.keys())
csvWriter.writerow(aStudent)
name,age,course
Ian,33,Computer Science
Terry,Unknown,Computer Science
Daniel,Unknown,Ethical Hacking and Cyber Security