Python file processing

Open file

open () function ʻOpen (filename [, mode [, encode [, error handling]]])`

Close file

close () function F.close

Read the file

read () function F.read ([integer size]) If an integer size is specified as an option, only the size will be read.

readline () function F.readline ([integer size]) Reads a line from the file and returns it as a string.

readlines () function F.readlines ([integer size]) Read multiple lines from a file.

** Read line by line from file and process **

f = open("test.txt", 'r', encoding='utf-8')
for line in f:
    print(line, end= " ")

Another option is to use the readlines () method, but this can cause problems when dealing with large files. Unless you have a specific reason, it's best to just add the file object to the for statement.

Write to file

write () function F.write (string) ** Open and export the file in the specified mode **

f = open("newfile.txt", "w", encodint="UTF-8")← Open file in mode w


f.write(s)← Write the string of variable s to a file
f.close ← Close the file

Recommended Posts

Python file processing
File processing in Python
Script python file
python image processing
Python distributed processing Spartan
File operations in Python
[Python] File / directory operations
[Note] File reading ~ Python ~
Python: Natural language processing
First Python image processing
Automatically execute python file
Reading pyc file (Python 3.5.2)
Text processing in Python
Queue processing in Python
File operations in Python
Image processing with Python
Read Python csv file
python external file reading
Python string processing illustration
Various processing of Python
Image processing with Python (Part 2)
100 Language Processing with Python Knock 2015
UTF8 text processing in python
"Apple processing" with OpenCV3 + Python3
Summary of python file operations
Python
Download the file in Python
Draw netCDF file with python
Acoustic signal processing with Python (2)
Acoustic signal processing with Python
Asynchronous processing (threading) in python
100 Language Processing Knock Chapter 1 (Python)
100 Language Processing Knock Chapter 2 (Python)
Image processing with Python (Part 1)
Image processing with Python (Part 3)
Image processing by python (Pillow)
Post processing of python (NG)
Image Processing Collection in Python
Download csv file with python
Using Python mode in Processing
[Python] Iterative processing (for, while)
[Python] Image processing with scikit-image
Execute Python script from batch file
Command line argument processing (Python docopt)
[Python] Random processing (create, select, sort)
Leave the troublesome processing to Python
Extract the xz file with python
Signal processing in Python (1): Fourier transform
File / folder path manipulation in Python
[Python] Easy parallel processing with Joblib
Python parallel processing (multiprocessing and Joblib)
Easy encryption of file contents (Python)
Tips on Python file input / output
[Python] Write to csv file with Python
Save the binary file in Python
[Automation with python! ] Part 1: Setting file
Implemented file download with Python + Bottle
100 Language Processing Knock with Python (Chapter 1)
Linebot creation & file sharing in Python
Paiza Python Primer 3: Learn Loop Processing
Output to csv file with Python