πŸ’‘
cheatsheets
  • πŸ‘‹Introduction
  • πŸ‹Docker
  • πŸ’ͺBrute Force
    • Hydra
  • πŸ—οΈCryptography
    • Generate pub/priv key
  • 🐧Linux
    • Curl
    • Debian
    • Fail2Ban
    • Find
    • Grep & Co
    • Netstat
    • ps
    • pdfcrack
    • qpdf
    • Rsync
    • Scp
    • Tmux
    • Ufw
    • Vim
  • 🐍Python
    • Files Handling
    • Web
  • πŸ‘οΈRecon
    • Cewl
    • DNS
    • Host Discovery
    • nmap
    • Web
  • πŸ”Splunk
    • tstats
  • πŸ“‘SSH
  • πŸ•ΈοΈWeb
    • Gobuster
    • OWASP
    • SQLi
      • Resources
  • ⛏️Resources
    • πŸ“‘Cheatsheets
    • πŸ‹οΈTrainings
Powered by GitBook
On this page
  • Functions
  • Open()
  • Read()
  • ReadLine()
  • ReadLines()
  • Write()
  1. Python

Files Handling

Functions

  • open() : returns a File object

  • read() and write() methods on a File object

  • close() : to close an open File

Open()

Read Mode (default)

f = open('myfile.txt') # is the same as: f=open('myfile.txt','r')

Write Mode

Opens file for writing, overwrites if file exists, creates if not.

f = open('myfile.txt','w')

Append Mode

Opens file for appending, creates if file does not exist.

f = open('myfile.txt','a')

Create Mode

Create file, error if file already exists.

f = open('myfile.txt','x')

Read()

Returns the whole text by default. You can specify how many characters you want into the parentheses.

all_text = f.read()
first_20_chars = f.read(20)

ReadLine()

Returns a line each time the method is called.

first_line = f.readline()
sec_line = f.readline()

ReadLines()

Returns a list containing all lines.

lines = f.readlines()
first = lines[0]
last = lines[-1]

Write()

content = "blablabla"
f.write(content)
PreviousPythonNextWeb

Last updated 1 year ago

🐍