Renaming files using Python
Do you ever find yourself in a situation at work or at home where you need to rename a bunch of Korean or txt files in bulk? In this case, Python makes it very easy to do what we need to do.
‘GPT-4 is free? (Feat. AI Service Alan)' post, let's use Alan to code some Python to automate the renaming of files within a specific folder.
In the previous post File creation with Python we're going to try renaming files using Python.
* If you want to see the pictures in the text in a little more detail, click on them to enlarge them.
Requesting Python source code written for you
First, you'll need to launch Alan, gaslight it (you're a Python guru~), and get started, right? (Note that for better prompting, the A guide to writing effective AI prompts (current through 2024): Leverage roles, actions, context, and formats Take your time reading the post. This is what Google suggests).
Or you can use the 'Past conversations' to bring up what we worked on in the previous post. I asked Alan to write the source code as shown below.
I have dozens of files with date-stamped text like 240310 in the filename, and they're in a specific folder, and I need to copy the entire folder first, and then code the names of the files in the copied folder so that 240310 is at the front.
Prompting Alan for the source code of the rename operation
In case of any errors, I copied the folder with the files I wanted to work on, and then ran the command above to work on the copied folder. I got the following response.

import os
import shutil
# Change to your desired folder path.
source_directory = 'source_directory'
destination_directory = 'destination_directory'
Copy the # folder.
shutil.copytree(source_directory, destination_directory)
# Rename the files in the copied folder.
for filename in os.listdir(destination_directory):
if '240310' in filename:
new_filename = '240310' + filename.replace('240310', '')
source = os.path.join(destination_directory, filename)
destination = os.path.join(destination_directory, new_filename)
os.rename(source, destination)Get your source code ready to run
Let's review for a second. Remember when we said that you only need to know three things to code?
First, connect to the AI service and use the Request codingand second, copy the generated source code into your integrated development environment (IDE) program using the Paste and runand third, if you don't get the results you want, work with Alan on the Complementing source code modifications .
Run VS Code and create a single Python file. In my case, I created 2nd_filename_change.py. Paste the above source code into VS Code and specify the source_directory and destination_directory. As shown below, I specified them as C:\source_code and C:\source_code\change respectively.

Run the source code
Now that you have your Python source code ready to run, you can use the Run button (▷) in the top right corner of VS Codeon the bottom of the page?
But then I thought about it, we have a folder called source_code, but not a folder called change under it. Is Python smart enough to do what we want it to do? Here's the result.

To our surprise, Python automatically created a folder called change and inside it, it created files that reflected what we wanted to do. You can see in the image below that the text 240310 is at the beginning of the filename.

Since the python file (.py) and the txt file you're working on are in the source_code folder, you can also see the jadeite that the python file is copied to the working folder (change). If you separate the working folder and the source code file folder, this won't happen, right?
Interpreting the source code

Today, we're going to take a step forward in our Python skills. Alan writes the source code and we've just been "gluing" it together. Now we're going to ask Alan to interpret it.
In the Alan window that generated the source code, as shown above, click "Can you please break down the above code line by line?" and Alan will give you a line-by-line explanation.
I think it's going to be a lot easier to teach yourself without going to a coding school or visiting related websites.
If you're like, "This isn't enough. I want to go deeper," you can ask Alan about the line-by-line stuff and he'll tell you more about it, or you can Google it yourself and find the relevant documentation, which is a more organized way to learn.
For example, you could ask Alan, "import os seems to import a module called os, what is the module called os?". Or you can google it yourself with the keyword 'python os module' and you'll get the The official description on the Python homepagein the following screenshot.
Organize
In this post, we've used the AI service to generate a code source for renaming a file and run it in VS Code to see the results.
And then we took it a step further and developed our ability to interpret the source code, which of course Alan did a great job of doing. And so we're getting comfortable with coding.
Python, you're not that hard to learn! Terry on secondlife.lol, secondlife.lol's second life assistantwas.
All content on 'secondlife.lol' is protected by copyright law. Unauthorized reprinting, copying, distribution, etc. is prohibited.






