We need to import the shutil module and state the initial path of the file and its destination. This is a screenshot of my photos directory before running our script. We will move it to a new folder called awesome and rename the picture awesome_sky.jpg. Let’s use the picture that we renamed red_sky.jpg. With shutil.move(), it is possible to not only rename a file but also change the file directory. Shutil helps to automate the process of copying and removing files and/or directories. Therefore, it does not need to be installed. The shutil module is also part of Python’s standard utility module. If this is the case, use shutil.move() instead. However, if the source and target locations are on different partitions, drives, or devices, os.rename() will not work. Now that we understand the mechanism behind the process of renaming a file in Python, it is important to note that renaming is like moving and vice-versa. Our file has been successfully renamed! You can find more information on os.rename() here as well as in our course on working with files and directories in Python. Let’s run our script and verify the output: This ensures the correctness of the path so that we can move and rename files in Python on Windows.įurthermore, the last character cannot be a backslash because it would throw a syntax error. It treats characters like backslashes as normal characters and not as escape ones. Os.rename(r'C:\py_scripts\photos\abhijeet-gaikwad-bokneofJXeI-unsplash.jpg', r'C:\py_scripts\photos\red_sky.jpg')Ī Python raw string is a normal string prefixed with the letter r. We will see later that we can use Python to get the file names in our directory. To get started, we can get the file name, type, and location with properties. As you can see, the file name is not very readable. Here is my initial photos folder.įirst, we are going to rename this beautiful picture of a red sky. For this tutorial, I collected some stock photos that we are going to rename in Python. To rename a single file, we need to enter the file's current name with its current location and its new name with its destination. Now that we have covered some basics, let’s apply the rename method from the os module to rename files in Python. When you explore a new concept, library, or framework, it is always a good idea to read the official documentation. ![]() If you are curious, you can read more about os.chdir() here and os.getcwd() here. To run a script, open your Python terminal, and run: Let’s write some code to change the directory with os.chdir() and display the current one with os.getcwd(). It is part of Python’s standard built-in utilities package, and no installation is required. The os module allows scripts to interact directly with the operating system. ![]() This will make it easier to understand how to rename files in Python later on. To warm ourselves up, let’s perform some essential manipulations using the os module. Essential Files and Directory Manipulations It is also a good idea but not compulsory to have some understanding of string manipulation with Python. Without further ado, let’s get right to it! To make sure that we are on the same page, I use Windows 10 and Python 3.8.5. ![]() At, we have your back! In this article, I will show you how to move and rename files in Python. If you don’t know how to do this, or if what I just said sounds like an uphill battle, fear not. One of these tasks is renaming and moving batches of files. It also helps me to improve my problem-solving skills. I find it worthwhile to spend time in the beginning to automate tasks that I will have to perform often to save time in the end. I abide by the concept of “smart laziness.” If you have to do something more than three times, write a script, and save yourself the hassle! ![]() You can do almost everything with it, which explains its widespread popularity.Įven if you are not a professional developer, you can improve your daily productivity with just a few lines of Python code. It is easy to learn due to its straightforward syntax, and it is also versatile. Python is a powerful programming language. In this article, you will learn how to use many of these modules and functions to rename and move files in Python. How much time do you spend renaming your files? Do you wonder if you could do it more quickly and easily? What if you could rename a huge number of files in the blink of an eye? Python comes with multiple built-in modules and functions to do exactly that.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |