What to do if you can't sort files with subscripts

Hello is white

When I try to sort such a file, I can sort it well in the file, but when I try to process it with Python etc.

The order may be different from what you expected. .. ..

I got stuck in this, so I will write it down as a memo for myself.

approach

Put an extra 0000 before the subscript. This will solve the problem.

However, it's a hassle to manually rewrite all the names, so let's leave it all to Python.

As a premise, consider only adding the subscripts of the original file.

ex)img32.png ⇒ img00032.png

Required knowledge

  1. Get the file name (using os)
  2. Get subscripts from names (use regular expressions)
  3. Rename the file (using shutil)

is. It's easy, so let's finish it one by one.

1. Get file name (os)

Use something called os to get the file name.

    import os
    file_dl = './File/'
    res = os.listdir(file_dl)

With this alone, I was able to get all the names of what was in the file that was file.

2. Obtaining a subscript from the name (regular expression re)

Next, let's get the subscript from the obtained file name.

    import re
    file_index = re.search(r'\d{1,}', 'img012.png').group()

Here, we use a regular expression to get only the numbers from the string.

In this case, only 012 is extracted from img012.png and stored in file_index.

3 Move files (shutil)

Finally, rename it and save it, using shutil. All I'm doing is renaming and moving it back to its original location.

    import shutil
    shutil.move(old_path,new_path)

By doing this, I was able to move what was in old_path to new_path.

Summary

If you connect the above

    import os
    import shutil
    
    file_dl = './File/'
    name = 'img_name'
    #Get the folder name in the file
    res = os.listdir(file_dl)
    
    #Extract the files one by one using the for statement.
    for target_name in res:
        #Combine file path and file name
        target_dl = file_dl + target_name
        #Extract only subscripts using regular expressions
        target_index = int(re.search(r'\d{1,}', target_name).group())
        #Create a new file name
        target_new_name = name+str("{0:05d}".format(target_index))+'.png'
        #Rename the file and save it again
        shutil.move(target_dl,file_dl+target_new_name)

Recommended Posts

What to do if you can't sort files with subscripts
What to do if you can't install pyaudio with pip #Python
What to do if you can't pipenv shell
What to do if you can't build your project with Maven
What to do if you can't find well with grep's -f option
What to do if you can't install with pip in babun environment
What to do if you can't pip install mysqlclient
What to do if you can't log in as root
What to do if you can't use WiFi on Linux
What to do if you get a UnicodeDecodeError with pip install
What to do if you can't use the trash in Lubuntu 18.04.
What to do if you can't find PDO in Laravel or CakePHP
What to do if you couldn't send an email to Yahoo with Python.
What to do if you can't use scikit grid search in Python
What to do if you get lost in file reference with FileNotFoundError
What to do if you get a TypeError with numpy min, max
What to do if you get Could not fetch URL 443 with pip
No module named What to do if you get'libs.resources'
ModuleNotFoundError: No module What to do if you get'tensorflow.contrib'
Links to do what you want with Sublime Text
What you can't do with hstack or vstack with dstack
What to do when you can't bind CaboCha to Python
What to do if you get angry with swapon failed: Operation not permitted
What to do if you get an error when installing python with pyenv
[AWS] What to do when you want to pip with Lambda
What to do if you get an OpenSSL error when installing Python 2 with pyenv
What to do with Magics install
What to do if you are addicted to Windows character code
What to do if you get "coverage unknown" in Coveralls
What to do with PYTHON release?
How to install and use pyenv, what to do if you can't switch python versions
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if you lose your EC2 key pair
Python | What you can do with Python
What to do if you can't hit the arrow keys in the Python interactive console
What to do if you run python in IntelliJ and end with an error
What to do if you get an Undefined error when trying to use pip with pyenv
What to do if you get a minus zero in Python
What to do if intellisense doesn't work with Anaconda + VSCode + Tensorflow2.1
What to do when you get "I can't see the site !!!!"
What you can do with API vol.1
What you can do with programming skills
Let's summarize what you want to do.
What to do if ipython and python start up with different versions
What to do if you forget your login password on Manjaro Linux
What to do if you get angry in TensorFlow v2 without attribute'app'
What to do if you get stuck during Anaconda installation on Linux
What to do if the Chainer (Windows) sample mnist terminates with WinError 183.
What to do if pyenv install does not proceed with an error
What to do if the server doesn't start with python manage.py runserver
What to do if an error occurs when importing numpy with VScode
What to do if you get an error when trying to load mnist
What to do if you get an error when installing Dlib (Ubuntu)
What to do if pipreqs results in UnicodeDecodeError
What to do if you get a "Wrong Python Platform" warning when using Python with the NetBeans IDE
Note: What to do if pip install fails
What to do if you get angry with "Value Error: unknown local: UTF-8" in python manage.py syncdb
What to do if pip cannot be installed
What to do if atom autocomplete-python doesn't work
What to do if Docker-sync suddenly stops working
What to do if "amazon-linux-extras" → "No module named amazon_linux_extras"