Home Backend Development Python Tutorial Python3读取文件常用方法实例分析

Python3读取文件常用方法实例分析

Jun 10, 2016 pm 03:11 PM
python3 read file

本文实例讲述了Python3读取文件常用方法。分享给大家供大家参考。具体如下:

''''' 
Created on Dec 17, 2012 
读取文件 
@author: liury_lab 
''' 
# 最方便的方法是一次性读取文件中的所有内容放到一个大字符串中: 
all_the_text = open('d:/text.txt').read() 
print(all_the_text) 
all_the_data = open('d:/data.txt', 'rb').read() 
print(all_the_data) 
# 更规范的方法 
file_object = open('d:/text.txt') 
try: 
  all_the_text = file_object.read() 
  print(all_the_text) 
finally: 
  file_object.close() 
# 下面的方法每行后面有‘\n'  
file_object = open('d:/text.txt') 
try: 
  all_the_text = file_object.readlines() 
  print(all_the_text) 
finally: 
  file_object.close() 
# 三句都可将末尾的'\n'去掉  
file_object = open('d:/text.txt') 
try: 
  #all_the_text = file_object.read().splitlines() 
  #all_the_text = file_object.read().split('\n') 
  all_the_text = [L.rstrip('\n') for L in file_object] 
  print(all_the_text) 
finally: 
  file_object.close() 
# 逐行读 
file_object = open('d:/text.txt') 
try: 
  for line in file_object: 
    print(line, end = '') 
finally: 
  file_object.close() 
# 每次读取文件的一部分 
def read_file_by_chunks(file_name, chunk_size = 100):   
  file_object = open(file_name, 'rb') 
  while True: 
    chunk = file_object.read(chunk_size) 
    if not chunk: 
      break 
    yield chunk 
  file_object.close() 
for chunk in read_file_by_chunks('d:/data.txt', 4): 
  print(chunk)

Copy after login

输出如下:

hello python
hello world
b'ABCDEFG\r\nHELLO\r\nhello'
hello python
hello world
['hello python\n', 'hello world']
['hello python', 'hello world']
hello python
hello worldb'ABCD'
b'EFG\r'
b'\nHEL'
b'LO\r\n'
b'hell'
b'o'

Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to read text file contents using File.ReadAllText function in C# How to read text file contents using File.ReadAllText function in C# Nov 18, 2023 pm 03:23 PM

How to use the File.ReadAllText function in C# to read the contents of a text file. In C# programming, we often need to read the contents of a text file. File.ReadAllText is a very convenient function that can help us quickly read the entire contents of a text file. This article will introduce how to use the File.ReadAllText function and provide specific code examples. First, we need to introduce the System.IO namespace in order to use the related methods of the File class.

PHP reads file content: steps to implement data import and parsing PHP reads file content: steps to implement data import and parsing Sep 06, 2023 pm 12:45 PM

Reading file contents with PHP: Steps to implement data import and parsing Importing and parsing file contents is one of the very common operations in web development. File importing and parsing can be easily achieved using PHP, and this article will describe the steps to achieve this process and provide code examples. Step 1: Select the file to import and parse In PHP, you first need to select the file to import and parse. You can use the file selection form or specify the file path manually. Here is sample code for a file selection form: <formmethod

Use the ioutil.ReadFile function to read the file contents and return a byte slice Use the ioutil.ReadFile function to read the file contents and return a byte slice Jul 26, 2023 pm 05:40 PM

Title: Use the ioutil.ReadFile function to read file contents and return byte slices Article content: In the standard library of the Go language, there is a very commonly used function ioutil.ReadFile(), which can be used to read from a specified file content and returns a byte slice. This function provides a simple and convenient way to read files and conveniently process the file contents for further processing. Below, we will show you how to use ioutil.R with a simple code example

How to read file contents using file_get_contents function in PHP How to read file contents using file_get_contents function in PHP Jun 26, 2023 pm 12:01 PM

In PHP, we often need to read data from files. In this case, we can use the file_get_contents function. This function can simply read everything from a file and return it as a string. This is very useful in many scenarios, such as reading configuration files, reading log files, etc. In this article, we will explain how to read file contents using the file_get_contents function in PHP. Step 1: Open the file using file

Analysis of Python's underlying technology: how to implement file reading and writing Analysis of Python's underlying technology: how to implement file reading and writing Nov 08, 2023 am 11:15 AM

Analysis of Python's underlying technology: How to implement file reading and writing, specific code examples are required. In Python programming, file operations are one of the most common and important operations. File reading and writing involves Python's underlying I/O technology. This article will explore how to use Python to implement file reading and writing operations, and provide specific code examples. 1. File reading Python provides a variety of methods for reading file contents. The most common ones include using the open() function and using the with statement. Use the open() function

Get all the knowledge about reading files in Python in one article Get all the knowledge about reading files in Python in one article Apr 11, 2023 pm 11:22 PM

Files are everywhere, no matter which programming language we use, handling files is essential for every programmer. File processing is a process used to create files, write data and read data from them, Python There are a wealth of packages for processing different file types, which makes it easier and more convenient for us to complete file processing work. Outline of this article: Use the context manager to open files. File reading mode in Python. Read text files. Read CSV files. Get JSON File Open File Before accessing the contents of the file, we need to open the file. Python provides a built-in function that helps us open files in different modes. The open() function accepts two bases

Use the ioutil.ReadFile function to read the file contents and return a string Use the ioutil.ReadFile function to read the file contents and return a string Jul 25, 2023 pm 10:41 PM

Title: Use the ioutil.ReadFile function to read file contents and return a string In Go language, there are many ways to read file contents and process them, one of them is to use the ReadFile function in the ioutil package. This article will introduce how to use the ioutil.ReadFile function to read a file and return its contents as a string. The ioutil.ReadFile function is a convenient method for reading file contents provided in the Go language standard library. it accepts a

Read file contents using PHP's file_get_contents() function Read file contents using PHP's file_get_contents() function Jun 27, 2023 pm 04:27 PM

In PHP programming, reading file contents is a basic task. PHP provides many functions to accomplish this task, one of the most popular is file_get_contents. It reads the entire file into a string for further processing. In this article, we will learn how to read file contents using file_get_contents function. Syntax The syntax of file_get_contents is as follows: stringfile_get_cont

See all articles