This is a summary of basic knowledge for working with files in Python.
Target: For those who have started learning Python Operating environment: Python3.8.3 (OS is MacOS)
--Specify the file to open and how to open it (read / write). --The file object will be returned. --After manipulating the file, you need to execute the close method (to free it from memory).
f = open("filename", "mode")
--Type of "mode"
mode | Description |
---|---|
r | Read mode. |
w | Overwrite mode. Discard the existing content. |
a | Addendum mode. Add to the end of the file. |
x | Write mode dedicated to creating new files. If the file exists, an error is returned and no writing is done. |
r+ | Read / write mode. |
If omitted, it will be treated as "r".
--"Mode" to handle file types
It can be specified in combination with the above "mode".
mode | Description |
---|---|
t | Text mode. The default mode for the open method. Change the line feed code from the platform-specific symbol to "\Convert to "n". |
b | Binary mode. It is recommended to use binary mode except for text files. |
The with keyword always closes the file object, so it's easier to write than implementing it with try ~ finally.
with open("filename") as f:
f.read()
f.read(size)
--Only the value specified for size is returned as a character string (in text mode) or bytes object (in binary mode). --If size is omitted or a negative number is specified, the entire contents of the file will be returned. --If the end of the file has been reached, an empty string ('') is returned.
>>>f.read(10)
'Hello Pyth'
f.readline()
--Reads only one line from the file and returns it. --Contains a newline character (\ n) at the end (excluding the last line that does not contain a newline character).
>>> f.readline()
'Hello Python\n'
--Files can be read efficiently for loop processing. --If you want to handle all the lines in the file in list format, use list (f) or f.readlines ().
>>> for line in f:
... print(line, end='')
...
Hello Python
Hello Java
Hello Ruby
f.write(string)
--Writes the contents of string to a file and returns the number of characters written. --It will be added every time you call the wite method until the file is closed.
>>> with open("sample.txt", "w") as f:
... f.write("Japan\n")
... f.write("USA\n")
... f.write("China\n")
...
6
4
6
>>> with open("sample.txt", "r") as f:
... f.read()
...
'Japan\nUSA\nChina\n'
f.tell()
--Returns an integer indicating the current location of the file.
--Return "0" if the current position is the first
>>> with open("sample.txt", "r") as f:
... f.tell()
...
0
――Reading 5 bytes moves the current position
>>> with open("sample.txt", "r") as f:
... f.read(5)
... f.tell()
...
'Japan'
5
f.seek(offset, whence)
--Calculate the file position by adding the offset value (offset) to the reference point (whence). --Specify 0: start, 1: current position, 2: end for whence. If omitted, it behaves as 0: beginning.
>>> with open("sample.txt", "rb+") as f:
... f.write(b"0123456789abcdef")
... f.seek(3) #3 bytes from the beginning
... f.read(1)
... f.seek(-1,2) #1 byte from the end
... f.read(1)
...
16
3
b'3'
15
b'f'
This time it was the basic content of the basics. Next, I would like to deepen my understanding of how to handle CSV and json (the library is provided).
Recommended Posts