From the course: Python: Working with Files

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Move and rename files with Python

Move and rename files with Python - Python Tutorial

From the course: Python: Working with Files

Move and rename files with Python

- [Instructor] Moving and renaming files are common operations in Python programs. For example, you may want to move current files into a separate directory to make space for new files. You also might want to rename a set of files to reference the date or year they were created in. When we move a file, we change its file path. Instead of home/desktop/example.png, we might move it to home/desktop/homework/example.png. In changing its path, we move the file to a new path. However, if we changed home/desktop/example.png to home/desktop/example1.png, we've renamed the file. Technically, we've moved it to this new file path, but in Python, we consider this operation renaming the file. We can rename a file using either the OS module or the pathlib module. For the OS module, we can use the rename method. The first input will be the current name of the file. In this case, we're going to rename monster01.png. The second input will be the…

Contents