0% found this document useful (0 votes)
20 views

Corisco - File Handling in Python

This document provides a cheat sheet on file handling in Python. It outlines the open(), read(), write(), and close() methods for working with files, including different file modes like read, write, append. It also discusses using seek() and tell() to manage file pointers, and lists OS and datetime modules for file manipulation and timestamps.

Uploaded by

Ashish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Corisco - File Handling in Python

This document provides a cheat sheet on file handling in Python. It outlines the open(), read(), write(), and close() methods for working with files, including different file modes like read, write, append. It also discusses using seek() and tell() to manage file pointers, and lists OS and datetime modules for file manipulation and timestamps.

Uploaded by

Ashish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

file handling in python Cheat Sheet

by corisco via cheatography.com/167022/cs/35648/

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

"b" - Binary Binary mode


pointers
"+" - Read and opens a file to read or write
Write mode seek(offset,whence=0)) changes the file position

Examples "r+" "a+b" "w+b" file.seek(4) change the pointer to the 4


file position
read()
[whence=] is optional, 0 0=start of file, 1=current position, 2=
is default file´s end
read() open() must have a read() method before.
file.read() tell() finds the file position

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")

use .strip() to remove "\n" os.remove("file.txt")

os.path.exists("file.txt") or os.path.i‐ returns True or False


sfile()

os.path.getsize("file.txt") return

os.path.getmtime("file.txt") returns a timestamp

os.path.abspath("file.txt") returns Absolute Path

os.getcwd show current


working directory

By corisco Published 7th December, 2022. Sponsored by CrosswordCheats.com


cheatography.com/corisco/ Last updated 7th December, Learn to solve cryptic crosswords!
2022. Page 1 of 2. http://crosswordcheats.com
file handling in python Cheat Sheet
by corisco via cheatography.com/167022/cs/35648/

Modules (cont)

os.mkdir("new_dir") create new directory in the current dir


os.chdr("dir") change working directory

os.rmdir("dir") removes empty directory / error if there is files


in it

os.listdir return a list of files e subdirectories in the dir

os.path.isdir

os.path.join

import datetime Datetime Module

datetime.date

os.path module https://docs.python.org/3/library/os.path.html


docs

os module docs https://docs.python.org/3/library/os.html

datetime module https://docs.python.org/3/library/datetime.html


docs

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

You might also like