Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to open files in txt format

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you how to use Python to open txt format files, I hope you have something to gain after reading this article, let's discuss it together!

How does Pythont open files in txt format?

1. First I use pycharm to create a project, and then create a python package in this project, and then create a demo1.txt file in it, which writes some novels I have read, and then use python to read the contents of this txt file.

txt reads as follows:

This txt file is created in the same package as the.py file:

The table of contents reads as follows:

Then how to read it, first you need to use open() and read().

The code is as follows:

c1 = open('demo1.txt', 'r')print(c1.read())

The 'r' in open() is opened in read mode, without which it is possible to read the contents of the file.

The code runs as follows:

And what you can see is, this is the whole file.txt has been read down.

You can also use python with … open … as …sentences to open files and rename files after opening.

The code is as follows:

with open('demo1.txt', 'r') as file1: contents = file1.read() print(contents)

After running, you can see that the code runs the same as the first time.

The results are as follows:

What is a relative path and what is an absolute path?

1. Relative path: relative to the directory (the directory where the program file is located), generally no drive letter starting

2. Absolute path: tell python the exact location of the file in the computer, usually starting with a drive letter

(The demo1.txt I created above is in the package of the program file, using open ('demo1.txt ', ' r') in the program) is the relative path, but if I use the absolute path, then the path used is (I created demo1.txt in drive C) Then I write it as follows:

open ('C\Demo1\demo1.txt ', ' r') III. How to extract data line by line? file2 = 'demo1.txt'with open(file2) as lines: for line in lines print("\n" + line.strip()) #strip() is used to remove spaces from strings. This can check the Internet related information.

Code Run Results:

4. Create a list containing the contents of each line of the file

With the keyword with, open() returns file objects that are available only within the with block. If you want to access the contents of a file outside of the with block, you can store the lines of the file in a list inside the with block and use that list outside of the with block: you can work on parts of the file right away, or you can defer them until later in the program.

Use readlines() to transfer each row read into this array.

The code is as follows:

with open(file2) as file_work: line3 = file_work.readlines()#line3 above is a list for line in line3: print(line)

The results are as follows:

One problem with the final run is that each line in the copied list has an extra newline character, making the distance between each line larger.

After reading this article, I believe you have a certain understanding of "how to use Python to open txt files". If you want to know more about this, please pay attention to the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report