Home > Software engineering >  How can I stop naming redundancies while renaming files in Python?
How can I stop naming redundancies while renaming files in Python?

Time:02-19

I am trying to rename a group of .txt files using Python. I can successfully rename the files to the new desired naming convention, but if the script gets run more than once or if some of the files in the directory already use the correct naming convention the file name starts becoming redundant. Here is my code for this process so far.

os.chdir('C:\Users\Phillip\Circuit_generation\Automation\Test_files\\')
sc_files = glob.glob('*sc.txt')
print('All netlist files ending with a series capacitor:')
for file_name in sc_files:
    order = int(file_name.count('sc')   file_name.count('sl')   file_name.count('pc')   file_name.count('pl'))
    old_name = file_name
    new_name = 'Order_{}_{}'.format(order, old_name)
    if os.path.isfile(new_name):
        pass
    else:
        os.rename(old_name, new_name)
    print(new_name)

The script is searching a directory and finding all files ending with specific text. Then I try looping through all the files that fit that criteria. The order of the circuit is calculated. I record the current file name then define a new name.

What I want is to end up with files in a format that reads something like:

Order_5_sc_pl_sl_pc_sc

However if this script runs more than once or a file already fits this naming convention I start getting file names such as:

Order_5_Order_5_sc_pl_sl_pc_sc

Order_5_Order_5_Order_5_sc_pl_sl_pc_sc

I have tried to check if the old name matches the new naming convention and pass if the name is already in the correct format, but I can't solve the problem. This code is my latest attempt to try to stop the files from being named incorrectly. I think this attempt is failing because I am not really allowing the old name to ever match the new name, but I can't seem to find a solution. I also tried the following but got similar results:

print('All netlist files ending with a series capacitor:')
for file_name in sc_files:
    order = int(file_name.count('sc')   file_name.count('sl')   file_name.count('pc')   file_name.count('pl'))
    old_name = file_name
    if os.path.isfile('Order_{}_{}'.format(order, old_name[8:])):
        pass
    else:
        new_name = 'Order_{}_{}'.format(order, old_name)
        os.rename(old_name, new_name)
    print(new_name)\

How can I go about renaming only the files that are necessary to rename and not continue creating long redundant file names? I am not sure what I am doing wrong and would greatly appreciate any help on the matter. Thank you.

CodePudding user response:

Can you give some example filenames you started out with so we can help you out? Also, please don't use the format function mostly everyone uses f-strings now and the "" is only used to crame multiple lines into one.

With what I got from your issue this is what I came up with. One side effect with my code could be the order of the orders could be changed ie: "sc_pl_sl_pl_sc" to "Order_sc_pl_pl_sl_sc"

import os

def get_all_orders(file_name):
    all_orders = []
    orders_amount = 0
    order_types = {"pl", "sl", "pc", "sc"}
    for order_type in order_types:
        order_type_amount = file_name.count(order_type)
        all_orders  = [order_type] * order_type_amount
    return orders_amount, all_orders


for file_name in ["Order_5_sc_pl_sl_pc_sc", "sc_pl_sl_pc_sc"]:
    if "Order_" not in file_name:
        orders_amount, all_orders = get_all_orders(file_name)
        new_filename = "Order_"   "_".join(all_orders)
        if not os.path.isfile(new_filename):
            # os.rename(file_name, new_name)
            print(f"os.rename({file_name}, {new_filename})")
  • Related