SoFunction
Updated on 2024-11-21

Example of code to read data from a file using a Python script

This article introduces the use of Python script to read data from the file code examples, the text of the sample code through the introduction of the very detailed, for everyone to learn or work with some reference to the learning value, you can refer to the next!

Recently, the progress of self-study Python is relatively slow, after work off and on looking at the efficiency is relatively low, it seems that we still need to be ruthless to improve a little bit every day.

Remember some time ago, Chen Dacat mentioned a mouthful of "the first implementation of python read local files", happened to see today's files and exceptions, combined with the exercise to organize the code to read local files with Python:

import os
#Import os modules from the standard library
 
('F:\HeadFirstPython\chapter3')
# Switch to the folder containing the data files
 
data = open('')
# Open a named file and assign the file to a file object named data
 
for each_line in data:
  print(each_line, end='')
# Loop to read a file with no content
 
()
#Close when file processing is complete

Iterate through each line of the file with a for loop until it is complete.

In fact, it also supports line-by-line reading, such as print((), end='').

This is the whole content of this article.