Python code example for reading and writing files
本篇文章给大家带来的内容是关于Python读写文件的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助
一.读取文件
读取文件步骤:
1.找到文件
2.打开文件
3.读取文件内容
4.关闭文件
打开方式:
r 以只读方式打开文件,文件的引用将会放在文件开头
rb 以二进制格式打开只读文件,文件的引用将会放在文件开头
r+ 以读写方式打开文件,文件的引用将会放在文件开头
w 以只写方式打开文件,如果该文件已经存在,则将其内容覆盖,如果不存在则会创建文件
wb 以二进制格式打开只写文件,如果该文件已经存在,则将其内容覆盖,如果不存在则会创建文件
w+ 以读写方式打开文件,如果该文件已经存在,则将其内容覆盖,如果不存在则会创建文件
a 打开一个文件用于追加,如果该文件已经存在,文件的引用将会放在文件的末尾,也就是说新的内容添加到已有内容之后。果不存在则会创建文件进行写入
a+ 打开一个文件用于读写,如果该文件已经存在,文件的引用将会放在文件的末尾,也就是说新的内容添加到已有内容之后。果不存在则会创建文件进行写入
filePath = r"C:\网络安全\python\测试\文件操作\1.txt" 1.txt 123456789 987654321 # 打开文件 fp = open(filePath,"r") # 打开二进制文件 fp = open(filePath,"rb") # 打开指定编码格式的文件 fp = open(filePath,"rb",encoding="GBK") # 错误处理:直接忽略 fp = open(filePath,"rb",encoding="utf-8",errors="ignore") # 读取文件全部内容 str1 = fp.read() print(str1) # 123456789 # 987654321 # 读取指定字节数 str2 = fp.read(8) print(str2) # 12345678 # 读取一整行,包括'\n' str3 = fp.readline() print(str3) # 123456789 # 读取所有行并返回列表 str4 = fp.readlines() print(str4) # ['123456789\n', '987654321'] # 关闭文件 fp.close() # 读文件的简写方式 with open('1.txt',"r") as fp: print(fp.read())
二.写文件
写文件步骤:
1、找到文件
2、打开文件
3、将内容写入缓冲区,此时内容没写写入文件
4、刷新缓冲区,直接把缓冲区的数据立刻写入文件
刷新缓冲区的方式:
a、程序结束
b、关闭文件
c、手动刷新
d、遇到\n
e、缓冲区满了
5、关闭文件
# 打开文件 fp = open('2.txt',"a") # 将内容写入缓存区 fp.write("This is a test!") # 刷新缓存区 fp.flush() # 关闭文件 fp.close() # 简写方式 with open('2.txt',"a") as fp: fp.write("hello") fp.close()
The above is the detailed content of Python code example for reading and writing files. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

Golang is more suitable for high concurrency tasks, while Python has more advantages in flexibility. 1.Golang efficiently handles concurrency through goroutine and channel. 2. Python relies on threading and asyncio, which is affected by GIL, but provides multiple concurrency methods. The choice should be based on specific needs.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
