Skip to Content
Mastering Python for Networking and Security
book

Mastering Python for Networking and Security

by José Manuel Ortega
September 2018
Intermediate to advanced
426 pages
10h 46m
English
Packt Publishing
Content preview from Mastering Python for Networking and Security

Opening a file

The classic way of working with files is to use the open() method. This method allows you to open a file, returning an object of the file type:

open(name[, mode[, buffering]])

The opening modes of the file can be r(read), w(write), and a(append). We can add to these the b (binary), t (text), and + (open reading and writing) modes. For example, you can add a "+" to your option, which allows read/write to be done with the same object:

>>> my_file=open("file.txt","r”)

To read a file, we have several possibilities:

  • The readlines() method that reads all the lines of the file and joins them in a sequence. This method is very useful if you want to read the entire file at once: >>> allLines = file.readlines().
  • If we want to read ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Python for Networking and Security - Second Edition

Mastering Python for Networking and Security - Second Edition

José Manuel Ortega
Python for Cybersecurity

Python for Cybersecurity

Howard E. Poston, III

Publisher Resources

ISBN: 9781788992510Supplemental Content