SoFunction
Updated on 2024-10-29

Python gets the paths of all the files in a folder in a small summary

In the process of Python programming, we often have such a need, need to get all the files in a file directory, or get all the files in a file directory with a specified suffix, or get the folder where the file is located, and may need to replace the file name, delete the folder. For these problems, this article summarizes these methods for your reference.

The folder for this example is placed on the desktop, the path is: C:\Users\Smile\Desktop\Weekly Reporting, there is a subfolder under the folder, and the subfolder has a pdf file.

1、Get the path of all files in the folder

It is recommended to use the listdir method under the os module, which can get the names of all the files under the folder (including the names of subfolders), and then work with the method to get the paths of all the files under the folder.

import os
dir_path = r'C:\Users\Smile\Desktop\Weekly Report'
file_ls = (dir_path)  # Get all file names
file_ls

The results of the run are as follows:

We can see that the results include not only the filename, theSubfolder names are also included.

Get the full path and just use 1 line of code on top of it:

file_ls = [(dir_path, file) for file in file_ls]
file_ls

As you can see in the image above, you can find the paths of all the files in the specified folder, including the subfolder paths.But what if we want to get the names of files in subfolders along with them?

The solution is to do this via the method. It returns 3 parameters - the root directory, all files in the root directory, and subfolders in the root directory. The magic of it is that after getting the root directory subfolders it will continue the loop and take out the files in the root directory subfolders in turn.

path = r'C:\Users\Smile\Desktop\Weekly Report'
for root, dirs, files in (path):
    print(root)

The result of the run is:

C:\Users\Smile\Desktop\Weekly report
C:\Users\Smile\Desktop\Weekly Report\Data

It can be found in the experiment looped twice, here specify the output of the current loop of the root directory, the first loop traversed the root directory of the file, because of the existence of subfolders under the root directory, so another loop in order to get the files under the subfolder, so the second loop output of the root directory for the subfolder's location.

For this, we can determine that if we specify an empty list outside the loop. By concatenating the filenames with the root directory and adding them to the empty list at each loop, we can get the paths of all the files in the root directory.

path = r'C:\Users\Smile\Desktop\Weekly Report'
file_ls = []
for root, dirs, files in (path):
    root_file_ls = [(root, file) for file in files]
    file_ls.append(root_file_ls)
file_ls

The results are as follows:

As you can see in the result, files located under subfolders are also added to the file path list.

2、Get the suffix name

Here you can use the method to split the path, which divides the path into two parts, one with the suffix name and the other with the part other than the suffix name.

file = r'C:\\\\ Users\\\\ Smile\\\ Desktop\\\\ Weekly Report\\\ Week 6 Summary_20221024_20221030.pptx'``houzhui = (file)[1]``houzhui``   ``--in the end--``'.pptx'

3、Get the file with the specified suffix

Paths are essentially strings, and you can use the endswith method to determine whether a path ends with a certain character. Now output all docx files under the direct path (without subfolders) of r'C:\Users\Smile\Desktop\Weekly Report'.

path = r'C:\Users\Smile\Desktop\Weekly Report'
file_ls = []
for root, dirs, files in (path):
    root_file_ls = [(root, file) for file in files]
    file_ls.append(root_file_ls)
# Because the above loop was done twice, the second time it was a file in a subfolder, so take out the 1st element
# Use endswith to determine whether it ends in docx or not.
file_ls = [file for file in file_ls[0] if ('.docx')]
file_ls

The results are as follows:

You can see that all the docx files have been output.

4. Modify the file name

Use (old_name, new_name) for this.

I won't give examples, it's too tiring~~~

A word of caution though, old_name and new_name are both paths.

5. Delete/move files

Delete files using (pathname)

Moving files requires the use of the shuilt library with the following syntax:

import shutil
(old_name, new_name)

6. Get the folder where the file is located

Use (path) to retrieve the

path = r'C:\\\\ Users\\\ Smile\\\ Desktop\\\\ Weekly Report\\\ Week 001 Summary_20220919_20220925.docx'
(path)

-Results-
'C:\\\\Users\\\\Smile\\\\Desktop\\\\Weekly Report'

7. Remove folder

1. If you use (path), the folder at this time must be an empty folder, otherwise it will report an error.

2. If it is a non-empty folder, use (path) to delete it successfully.

The main operations for file paths are: get file path, get suffix, get file with specified suffix, delete/move file, replace file name, get folder where file is located, remove folder.

This article on Python to get the path of all the files under the folder summary of the article is introduced to this, more related to Python to get the path of all the files content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future more!