Corisco - File Handling in Python
Corisco - File Handling in Python
open() write()
open() ("filename","mode",enconding="UTF-8") write() open method "a" will append to end of file
modes open method "w" will overwrite the entire file
"r" - Read (default) opens a file for reading, error if doesn´t exist file.write("text")
"a" - Append opens a file for appending, creates the file if create a new open method "x" will create a file, error if it exists
doesn´t exist file
"w' - Write opens a file for writing, creates the file if open method "a" will create a file if doesn´t exist
doesn´t exist open method "w" will create a file if doesn´t exist
"x" - Create creates the specified file, error if the file exists writelines() turns iterable object(lists,tuples,dict...) into lines in
"t" - Text (default) Text mode the file
file.read(5) reads the first 5 characters file.tell() returns the file position
readline() close()
file.readline() returns one line of the file
close() always close the file when done
file.readline(5) reads the fist 5 characters of the line file.close()
file.readline() / file.read‐ calling readline two times, reads the two
line() first lines
Modules
readlines() returns a list, each item is a line as
a string import os OS module
for line in file loops through the chars or lines of the file os.rename("oldname.txt", "new_name.txt")
os.path.getsize("file.txt") return
Modules (cont)
os.path.isdir
os.path.join
datetime.date
By corisco
Published 7th December, 2022. Sponsored by CrosswordCheats.com
cheatography.com/corisco/
Last updated 7th December, Learn to solve cryptic crosswords!
2022. Page 2 of 2. http://crosswordcheats.com