- Hands-On Penetration Testing with Python
- Furqan Khan
- 603字
- 2021-07-02 14:14:01
File access and manipulation
We can read, write, and update files in Python. Python has got an open construct that can be used to provide file manipulation operations. When we open a file, there are various modes in which that file can be opened, as shown follows:
- r: Read mode, this reads the file in text mode (Default).
- rb: This reads the file in binary mode.
- r+: This reads the file in both read and write mode.
- rb: This opens the file for reading and writing in binary mode.
- w: This opens the file in write mode only. It overwrites the existing file.
- wb: This opens the file for writing in binary mode. It overwrites the existing file.
- w+: This opens the file in both write and read mode. It overwrites the existing file.
- wb+: This opens the file for both reading and writing in binary mode. It overwrites the existing file.
- a: This opens the file in append mode and creates a file if it doesn't exist.
- ab: This opens the file in append binary mode and creates a file if it doesn't exist.
- a+: This opens the file in both append and read mode and creates a file if it doesn't exist.
- ab+: This opens the file in append read binary mode and creates a file if it doesn't exist.
In the following code block, the first argument to the open method call is the path of the file to be opened. The second is the mode in which the file has to be opened, and the third is the optional buffering argument that specifies the file's desired buffer size: 0 means unbuffered, 1 means line-buffered, and any other positive value means use a buffer of (approximately) that size (in bytes). A negative buffering means that the system default should be used. This is usually line-buffered for tty devices and fully buffered for other files. If omitted, the system default is used.
open("filepath","mode",buffer)
With buffering, instead of reading directly from the operating system representation of the raw file (which would have high latency), the file is instead read into a OS buffer and read from there from then on. The advantage of this is that if we have a file present on the shared network and our objective is to read the file every 10 ms. We can load it once in the buffer and then read it from there, instead of reading it from the network each time, which would be expensive.
Take a look at the following snippet from the File_access.py file to understand more:

The code snippet in the preceding screenshots from the File_access.py file explains how to use files in Python. The read() method of the File class takes the file path and if the whole path is not given, then the current working directory is assumed to be the starting path. The read() method invoked on the file instance reads the whole file into the program variable. read(20) will load 20 bytes from the file in the current file pointer position. This is very handy when we are dealing with large files.
The readlines() method returns a list, with each entry referring to each line of the file. The readline() method returns the current line from the file. The seek() method will take the file pointer to the position specified in the argument. Therefore, whenever we execute seek(0), the file pointer points towards the beginning of the file:

- Visual C++程序設計教程
- 少年輕松趣編程:用Scratch創作自己的小游戲
- 樂學Web編程:網站制作不神秘
- oreilly精品圖書:軟件開發者路線圖叢書(共8冊)
- C語言從入門到精通(第4版)
- 軟件工程
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- Hands-On Functional Programming with TypeScript
- Python機器學習基礎教程
- Visual C#.NET程序設計
- 響應式架構:消息模式Actor實現與Scala、Akka應用集成
- Java程序員面試筆試寶典(第2版)
- Illustrator CC平面設計實戰從入門到精通(視頻自學全彩版)
- SQL Server 2016 從入門到實戰(視頻教學版)
- UML2面向對象分析與設計(第2版)