Detailed explanation of file operations in python3
Steps: Open the file-"Operate the file-"Close the file
Open the file
文件句柄 = open('文件路径', '模式')
Specify the file encoding
文件句柄= open('文件路径','模式',encoding='utf-8')
In order to prevent forgetting to close the file, you can use the context manager to open the file
with open('文件路径','模式') as 文件句柄:
The modes for opening files are:
r, read-only mode (default).
w, write-only mode. [Unreadable; create if it does not exist; delete the content if it exists;]
a, append mode. [Readable; if it does not exist, create it; if it exists, only append the content;]
r+, the file can be read and written. [Readable; Writable; Appendable]
w+, write and read
"U" means that when reading, you can r \n \r\n is automatically converted to \n (same as r or r+ mode)
rU
r+U
"b" means processing binary files (such as: FTP sending and uploading ISO image files, Linux can be ignored, Windows needs to be marked when processing binary files)
rb
- ##wb
- ab
Close file
文件句柄.close()
Operation file:
detach#占位
fid = 文件句柄.fileno()print(fid)
文件句柄.flush()
文件句柄.isatty()
str = 文件句柄.read() #读取整个文件 str1 = 文件句柄.read(10) #读取文件前10个字符
文件句柄.readable()
print(文件句柄.readline()) #读取第一行数据print(文件句柄.readline(3)) #读取第二行前3个字符print(文件句柄.readline()) #读取第二行剩余字符print(文件句柄.readline()) #读取第三行
文件句柄.seek(6)
文件句柄.seekable()
文件句柄.tell()
f = open('text.txt','r+',encoding='utf-8') f.seek(9) #把指针移动到第9个字节后面(即第3个中文后面)f.truncate() #把第3个中文后面的字符删除,并写入文件f.close()
文件句柄.writable()
文件句柄.write('字符串')
The above is the detailed content of Detailed explanation of file operations in python3. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

A gho file is an image file created by NortonGhost software and used to back up and restore the operating system and data. In some cases, you can delete gho files, but do so with caution. This article will introduce the role of gho files, precautions for deleting gho files, and how to delete gho files. First, let's understand the role of gho files. A gho file is a compressed system and data backup file that can save an image of an entire hard disk or a specific partition. This kind of backup file is usually used for emergency recovery

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Reading and writing files safely in Go is crucial. Guidelines include: Checking file permissions Closing files using defer Validating file paths Using context timeouts Following these guidelines ensures the security of your data and the robustness of your application.

The Go language provides two methods to clear file contents: using io.Seek and io.Truncate, or using ioutil.WriteFile. Method 1 involves moving the cursor to the end of the file and then truncating the file, method 2 involves writing an empty byte array to the file. The practical case demonstrates how to use these two methods to clear content in Markdown files.

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

As a programming language widely used in the field of software development, C language is the first choice for many beginner programmers. Learning C language can not only help us establish the basic knowledge of programming, but also improve our problem-solving and thinking abilities. This article will introduce in detail a C language learning roadmap to help beginners better plan their learning process. 1. Learn basic grammar Before starting to learn C language, we first need to understand the basic grammar rules of C language. This includes variables and data types, operators, control statements (such as if statements,
