1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Renaming files in python, and sorting out files with only numbers [closed]

Discussão em 'Python' iniciado por Stack, Setembro 17, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to rename all files in a folder, and sort the ones that are only named numbers. I'm removing some brackets "[]" from the names.

    But I'd also like to sort out all the files that have only numbers in them. I tried using isNumerical on the names but i forgot to think about the file extension...

    Is there a "simple" way of finding all the files that only have numbers in their name and removing them? I might have to loop through a couple of times to remove the brackets, remove the file extension, check if numerical and then put the file extension back? I have little faith this would work though?

    The code I have so far:

    (Mind you, theres some norwegian syntax- dont worry 'bout it ;) )

    import os
    from os import path
    import shutil
    import os, sys


    folder = input("Paste file-path to folder with files to rename:")
    destination = os.path.join(folder + "\\resultat")
    FileList = os.listdir(folder)

    for files in FileList:
    if '[' in files:
    NewName = files.replace("[", " ")
    os.rename(os.path.join(folder, files), os.path.join(folder, NewName))

    for files in FileList:
    if ']' in files:
    NewName = files.replace("]", "")
    os.rename(os.path.join(folder, files), os.path.join(destination, NewName))



    This runs through the folder, removing all the brackets and saving the results to a new folder. If anyone's curious- the files are 3d files of parts of a cabinet. The files are from blum.com. But I'm running them through some software that makes machining paths for a CNC machine. So im trying to automate as many of the steps as possible. Manually renaming files is an easy task to tackle :)

    Continue reading...

Compartilhe esta Página