Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use os.listdir and os.walk to obtain File path in python

Shulou Source: shulou.com Published: 2022-06-01 15:29:38 10月04日 Update

This article will explain in detail how to use os.listdir and os.walk to obtain file paths in python. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Case 1: there are only files and no folders under a directory. You can use os.listdir at this time.

There is a file directory (folder) on our desktop with three files in it.

File (dir) |

-| test1.txt-- | test2.txt-- | test3.txt

Use the following program to get the absolute path to the file:

Import os

Path = ritual C:\ Users\ Administrator\ Desktop\ file'

For filename in os.listdir (path):

Print (os.path.join (path,filename))

Use os.listdir to read all the file names under a directory, and then use os.path.join to combine the path of the directory with the file name to get the dead-end path of the file. the result is as follows:

C:\ Users\ Administrator\ Desktop\ file\ test1.txt

C:\ Users\ Administrator\ Desktop\ file\ test2.txt

C:\ Users\ Administrator\ Desktop\ file\ test3.txt

Case 2: in the case of recursion, there are both directories and files under a directory (there may also be directories and files under the directory). How to read all the files in it, use os.walk:

Os.walk introduction:

Let's create a file directory on the desktop with the following organizational structure:

File (dir):

-| file1 (dir):-| file1_test1.txt-| file1_test2.txt-| file2 (dir)-| file2_test1.txt-| file_test1.txt-| file_test2.txt

Run the code:

Import os

Path = ritual C:\ Users\ Administrator\ Desktop\ file'

For dirpath,dirnames,filenames in os.walk (path):

Print (dirpath,dirnames,filenames)

The output is as follows:

C:\ Users\ Administrator\ Desktop\ file ['file1',' file2'] ['file_test1.txt',' file_test2 .txt']

C:\ Users\ Administrator\ Desktop\ file\ file1 [] ['file1_test1.txt',' file1_test2.txt']

C:\ Users\ Administrator\ Desktop\ file\ file2 [] ['file2_test1.txt']

Os.walk enters a path name and returns a triple of dirpath, dirnames, filenames in the form of yield (actually a generator)

Dirpath is the path to the directory and is a string. For example, C:\ Users\ Administrator\ Desktop\ file and C:\ Users\ Administrator\ Desktop\ file\ file1 above.

Dirnames lists the names of all directories that exist under the directory path. For example, if there are two directories under C:\ Users\ Administrator\ Desktop\ file: file1 and file2, it lists the directory names under this directory path.

Filenames lists the names of all files under the directory path. There are also two files file_test1.txt and file_test2 .txt under C:\ Users\ Administrator\ Desktop\ file, then the program will list these two file names.

How to get all the file paths under a path:

Import os

Path = ritual C:\ Users\ Administrator\ Desktop\ file'

For dirpath,dirnames,filenames in os.walk (path):

For filename in filenames:

Print (os.path.join (dirpath,filename))

The results are as follows:

C:\ Users\ Administrator\ Desktop\ file\ file_test1.txt

C:\ Users\ Administrator\ Desktop\ file\ file_test2 .txt

C:\ Users\ Administrator\ Desktop\ file\ file1\ file1_test1.txt

C:\ Users\ Administrator\ Desktop\ file\ file1\ file1_test2.txt

C:\ Users\ Administrator\ Desktop\ file\ file2\ file2_test1.txt

On how to use os.listdir and os.walk in python to get the file path is shared here, I hope 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.

Tags: File directory path name two case file name result content folder article more desktop knowledge program article noodle good three code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft Docker NVidia Xiaomi