How do I use a list or set as keys in file renaming

2024/11/16 7:29:07

Is something like this possible? Id like to use a dictionary or set as the key for my file renamer. I have a lot of key words that id like to filter out of the file names but the only way iv found to do it so far is to search by string such as key720 = "720" this make it functions correctly but creates bloat. I have to have a version of the code at bottom for each keyword I want to remove.

how do I get the list to work as keys in the search? I tried to take the list and make it a string with:

str1 = ""
keyres = (str1.join(keys))

This was closer but it makes a string of all the entry's I think and didn't pick up any keywords.

so iv come to this at the moment.

keys = ["720p", "720", "1080p", "1080"]for filename in os.listdir(dirName):if keys in filename:    filepath = os.path.join(dirName, filename)newfilepath = os.path.join(dirName, filename.replace(keys,""))os.rename(filepath, newfilepath)

Is there a way to maybe go by index and increment it one at a time? would that allow the strings in the list to be used as strings?

What I'm trying to do is take a file name and rename it by removing all occurrences of the key words.

Answer

How about using Regular Expressions, specifically the sub function?

from re import subKEYS = ["720p", "720", "1080p", "1080"]old_filename = "filename1080p.jpg"
new_filename = sub('|'.join(KEYS),'',old_filename)print(new_filename)
https://en.xdnf.cn/q/119555.html

Related Q&A

How to change Python comment font style in the latest VS Code? [duplicate]

This question already has an answer here:How to change the font-style of code comments in vscode?(1 answer)Closed 6 months ago.Seems like with the latest VS Code update, all the comment font style has…

How to crop an image based on a complex criteria?

I have a set of similar images like the one below. I want to keep the portion of the image that is within the top red irregular rectangle (green arrows represent the space that I want to keep; anything…

Getting current video tag URL with selenium

Im trying to get the current html5 video tag URL using selenium (with python bindings):from selenium import webdriverdriver = webdriver.Chrome() driver.get(https://www.youtube.com/watch?v=9x6YclsLHN0)…

How to determine if two rows are identical (similar) if row 2 contains part of the info from row 1?

Hope you are having a good day. I am currently working with an extremely dirty dataframe containing First Name, Last Name, and Middle Name. One the issues that I am trying to resolve looks like below:F…

Cartopy fancy box

Hello I have been trying to plot data in a Orthographic projection. The data is plotted but I want the box to follow the data limits. Like in this example I am sharing form M_map[enter image descriptio…

discord.py - No DM sent to the user

I am making a discord.Client. I have a DM command that sends a DM to a specific user, but no message is sent to the user when the command is run, but a message is sent on the Context.channel. Here is m…

Improve CPU time of conditional statement

I have written an if-elif statement, which I believe not be very efficient:first_number = 1000 second_number = 700 switch = {upperRight: False,upperLeft: False,lowerRight: False,lowerLeft: False,middle…

Why no colon in forming a list from loop in one line in Python?

From this website, there is a way to form a list in Python from loop in one line squares = [i**2 for i in range(10)]My question is, typically, after a loop, there is a colon, e.g., squares = [] for i i…

Merge each groups rows into one row

Im experienced with Pandas but stumbled upon a problem that I cant seem to figure out. I have a large dataset ((40,000, 16)) and I am trying to group it by a specific column ("group_name" for…

Python decode unknown character

Im trying to decode the following: UKLTD� For into utf-8 (or anything really) but I cannot workout how to do it and keep getting errors likeascii codec cant decode byte 0xae in position 8: ordinal not…