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 read resource files in Python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces you how to read resource files in Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.

We know that when you put a resource file and a .py file together, you can read it directly in the .py file using the file name. For example:

With open ('test.txt') as f: content = f.read () print (' the content in the file is:', content)

The running effect is shown in the following figure:

But please note that here I am running the read.py file directly. What if the resource file is stored in a package, and then we call the .py file in the package outside? Let's have a try:

As you can see, Python can't find this file now. This is because our entry program is in the ~ / get_title folder and the test.txt file is in the ~ / get_title/util folder. Because we are running main.py, Python will look for test.txt in the ~ / get_title folder and will not find it.

If you are referring to other modules in the package, you can use relative paths. For example, if we refer to the conn object in the same package called sql_util.py, we can directly write it as from. SQL _ util import conn. However, resource files cannot be read using relative paths, as shown in the following figure:

One stupid way is to get the folder where the line of code that is currently running is located, and then spell out the full path to the resource file. Modify the read.py file:

Novice learning, Python tutorials / tools / methods / troubleshooting + V:itz992import os def read_file (): current_folder = os.path.dirname (_ _ file__) resource_path = os.path.join (current_folder, 'test.txt') with open (resource_path) as f: content = f.read () print (' contents in the file are:', content)

The running effect is shown in the following figure:

But it's a little troublesome to write like this.

If your Python version is no less than 3.7, you can use importlib.resources to quickly read resource files:

From importlib import resources with resources.open_text ('package name', 'resource path') as f: content = f.read ()

The running effect is shown in the following figure:

If you are not reading a text file, you can change resources.open_text to resources.open_binary to read the binary file.

It is important to note, however, that the resource files must be placed in the root of the package. So that it can be read correctly. If the resource file is in a subdirectory inside the package, importlib.resources cannot be read directly.

For example, our package is util, in which there is a folder called deep_folder, and the resource file test.txt is placed in deep_folder. At this point, if we want to read this resource file, we must create a _ _ init__.py in the deep_folder folder and turn it into a package. Then modify the code of read.py:

From importlib import resources from. Import deep_folder def read_file (): with resources.open_text (deep_folder, 'test.txt') as f: content = f.read () print (' contents in the file are:', content) novice learning, Python tutorials / tools / methods / troubleshooting + V:itz992

Import deep_folder as a module, and then take this module as the first parameter of resources.open_text. This can be read correctly, as shown in the following figure:

On how to read resource files in Python to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report