Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does python replace the file name

Shulou Source: shulou.com Published: 2022-05-31 11:15:39 09月17日 Update

This article mainly introduces the relevant knowledge of "how to replace the file name with python". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this article "how to replace the file name with python" can help you solve the problem.

The specific steps are as follows:

1. First, open python and create a new python project

After the 2.python project is created, import the os module in the project using the import method

Import os

After the 3.os module is imported, define a variable in the project to store the file path and create the file object

File = "D:\ User\ test.txt"

4. After the file object is created, use the os.path.basename () method to get the file name

Res = os.path.basename (file)

5. Finally, once the file name is obtained, the file name can be replaced using the os.rename () method

Os.rename (res,newfile)

This is the end of the introduction to "how to replace the file name with python". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Files methods projects names knowledge names objects modules industries built different practical content variables practicality actual file names articles more cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Linux Huawei NVidia Shulou Technology