How to Upload a Txt File to Python

Summary: in this tutorial, you lot learn various ways to read text files in Python.

TL;DR

The post-obit shows how to read all texts from the readme.txt file into a cord:

            

with open('readme.txt') as f: lines = f.readlines()

Code language: JavaScript ( javascript )

Steps for reading a text file in Python

To read a text file in Python, you follow these steps:

  • First, open a text file for reading past using the open() function.
  • Second, read text from the text file using the file read(), readline(), or readlines() method of the file object.
  • Third, close the file using the file close() method.

i) open() role

The open up() function has many parameters just you'll be focusing on the beginning two.

            

open(path_to_file, mode)

The path_to_file parameter specifies the path to the text file.

If the file is in the same folder as the program, you lot simply need to specify the name of the file. Otherwise, you demand to specify the path to the file.

To specify the path to the file, you use the forward-slash ('/') even if y'all're working in Windows.

For example, if the file is readme.txt stored in the sample folder equally the plan, you need to specify the path to the file every bit c:/sample/readme.txt

The manner is an optional parameter. It'southward a cord that specifies the mode in which you want to open up the file.

The following table shows available modes for opening a text file:

Mode Clarification
'r' Open for text file for reading text
'w' Open up a text file for writing text
'a' Open a text file for appending text

For example, to open a file whose name is the-zen-of-python.txt stored in the same binder as the program, yous use the following lawmaking:

            

f = open('the-zen-of-python.txt','r')

Code language: JavaScript ( javascript )

The open() part returns a file object which y'all will utilize to read text from a text file.

2) Reading text methods

The file object provides you lot with 3 methods for reading text from a text file:

  • read() – read all text from a file into a cord. This method is useful if you lot have a small file and yous want to manipulate the whole text of that file.
  • readline() – read the text file line by line and return all the lines as strings.
  • readlines() – read all the lines of the text file and return them equally a list of strings.

three) close() method

The file that y'all open will remain open up until you close information technology using the close() method.

It's important to shut the file that is no longer in use. If you don't close the file, the program may crash or the file would exist corrupted.

The following shows how to call the close() method to close the file:

            

f .close()

Code language: CSS ( css )

To close the file automatically without calling the close() method, y'all apply the with argument like this:

            

with open(path_to_file) as f: contents = f.readlines()

Code language: JavaScript ( javascript )

In practise, y'all'll use the with statement to shut the file automatically.

Reading a text file examples

We'll use the-zen-of-python.txt file for the demonstration.

The post-obit example illustrates how to utilize the read() method to read all the contents of the the-zen-of-python.txt file into a string:

            

with open up('the-zen-of-python.txt') as f: contents = f.read() print(contents)

Lawmaking language: JavaScript ( javascript )

Output:

            

Cute is improve than ugly. Explicit is ameliorate than implicit. Simple is improve than complex. ...

The following case uses the readlines() method to read the text file and returns the file contents as a list of strings:

            

lines = [] with open up('the-zen-of-python.txt') equally f: lines = f.readlines() count = 0 for line in lines: count += 1 print(f'line {count}: {line}')

Code linguistic communication: JavaScript ( javascript )

Output:

            

line ane: Cute is ameliorate than ugly. line ii: Explicit is better than implicit. line 3: Simple is amend than circuitous. ...

The following instance shows how to utilize the readline() to read the text file line past line:

            

with open('the-zen-of-python.txt') as f: line = f.readline() while line: line = f.readline() print(line)

Lawmaking language: JavaScript ( javascript )

Output:

            

Explicit is better than implicit. Elementary is better than circuitous. Circuitous is better than complicated. ...

A more than concise way to read a text file line past line

The open() role returns a file object which is an iterable object. Therefore, yous can use a for loop to iterate over the lines of a text file as follows:

            

with open up('the-zen-of-python.txt') equally f: for line in f: print(line)

Lawmaking language: JavaScript ( javascript )

This is more concise way to read a text file line by line.

Read UTF-8 text files

The code in the previous examples works fine with ASCII text files. However, if you're dealing with other languages such as Japanese, Chinese, and Korean, the text file is not a simple ASCII text file. And it'due south likely a UTF-eight file that uses more than just the standard ASCII text characters.

To open a UTF-8 text file, you need to pass the encoding='utf-eight' to the open up() part to instruct it to expect UTF-8 characters from the file.

For the sit-in, you'll use the following quotes.txt file that contains some quotes in Japanese.

The post-obit shows how to loop through the quotes.txt file:

            

with open('quotes.txt', encoding='utf8') as f: for line in f: print(line.strip())

Code linguistic communication: JavaScript ( javascript )

Output:

Python read utf-8 text file

Summary

  • Employ the open() function with the 'r' mode to open a text file for reading.
  • Use the read(), readline(), or readlines() method to read a text file.
  • Always shut a file after completing reading information technology using the shut() method or the with statement.
  • Use the encoding='utf-eight' to read the UTF-8 text file.

Did you find this tutorial helpful ?

hugheshadvingrow.blogspot.com

Source: https://www.pythontutorial.net/python-basics/python-read-text-file/

0 Response to "How to Upload a Txt File to Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel