I'm trying to rename the files in a directory that have the substring "Episode.26" by truncating the words before and after the substring
e.g. 'The.Vision.of.Escaflowne.Episode.26.Eternal.Love.1080p.Dual.Audio.Bluray [BC6DDF99].mkv'
The value to be found will always be Episode.## (## two digits)
Desired result: Episode.26.mkv
Current result: Episode.26.Eternal.Love.1080p.Dual.Audio.Bluray [BC6DDF99].mkv'
I removed the first n characters using python; but, I don't know how to isolate 'Episode.26' efficiently
import os
key = "Episode."
for filename in os.listdir("."):
if(key in filename):
index = filename.index(key)
os.rename(filename, filename[index:])
If your filename is always separated by periods then split() might be sufficient:
import os
ext = ".mkv"
ndl = "Episode"
for filename in os.listdir("."):
if ext in filename and ndl in filename:
num = filename.split(ndl, 1)[1].split(" ")[0].split(".")[1]
epi = "{}.{}{}".format(ndl, num, ext)
os.rename(filename, epi)
This should split the name after your needle ("ndl") grab the episode number and rename the file; it should also handle filenames that include spaces in addition to periods or if "Episode.26" is at the end of the string (eg. Some.Movie.Episode.26 [BC6DDF99].mkv).
Result:
Episode.26.mkv
If u'r sure there's two digits after "Episode.", then u can code like this. Otherwise, i'm afraid u should use re or split to get what u want.
import os
key = 'Episode'
for filename in os.listdir("."):
try:
index = filename.index(key)
_, file_extension = os.path.splitext(filename)
new_name = filename[index:index+len(key)+3] + file_extension
os.rename(filename, new_name)
except ValueError:
pass
You could use regular expressions, capture the episode number and file extension and create the new name using such data:
Code
import re
import os
key = "Episode"
regexp = re.compile('.*%s\.(\d\d).*\.(.+)' % key)
for filename in os.listdir("."):
match = regexp.match(filename)
if match:
episode, file_ext = match.group(1), match.group(2)
new_name = key + episode + '.' + file_ext
os.rename(filename, new_name)
This way is more cleaner and flexible. REs are very powerfull. Let me know if this worked for you.
in your code, you can use search instead of match and remove .* at the beginning of re
import re
import os
key = "Episode"
regexp = re.compile('%s\.(\d\d).*\.(.+)' % key)
for filename in os.listdir("."):
match = regexp.search(filename)
if match:
episode, file_ext = match.group(1), match.group(2)
new_name = key + episode + '.' + file_ext
os.rename(filename, new_name)
Related
I have files with filenames as "lsud-ifgufib-1234568789.png" I want to rename this file as digits only which are followed by last "-" and then save them back in the folder.
Basically I want the final filename to be the digits that are followed by "-".
~ path = 'C:/Users/abc/downloads'
for filename in os.listdir(path):
r = re.compile("(\d+)")
newlist = filter(r.match, filename)
print(newlist)
~
How do I proceed further?
Assumptions:
You want to rename files if the file has a hyphen before the number.
The file may or may not have an extention.
If the file has an extention, preserve it.
Then would you please try the following:
import re, os
path = 'C:/Users/abc/downloads'
for filename in os.listdir(path):
m = re.search(r'.*-(\d+.*)', filename)
if m:
os.rename(os.path.join(path, filename), os.path.join(path, m.group(1)))
You could try a regex search followed by a path join:
import re
import os
path = 'C:/Users/abc/downloads'
for filename in os.listdir(path):
os.rename(filename, os.path.join(path, re.search("\d+(?=\D+?$)", filename).group()))
import re
import pathlib
fileName = "lsud-ifgufib-1234568789.png"
_extn = pathlib.Path(fileName).suffix
_digObj = re.compile(r'\d+')
digFileName = ''.join(_digObj.findall(fileName))
replFileName = digFileName + _extn
I have a Python script that will walk through all of the directories within the Test Folder(in this case) and will remove all numbers at the beginning of each of the file names. So my question is how would I modify my script in order to remove numbers from the whole file name? Not just the beginning or the end of it.
Thanks,
Alex
import os
for root, dirs, files in os.walk("Test Folder", topdown=True):
for name in files:
if (name.startswith("01") or name.startswith("02") or name.startswith("03") or name.startswith("04") or name.startswith("04") or name.startswith("05") or name.startswith("06") or name.startswith("07") or name.startswith("08") or name.startswith("09") or name[0].isdigit()):
old_filepath = (os.path.join(root, name))
_, new_filename = name.split(" ", maxsplit=1)
new_filepath = (os.path.join(root, new_filename))
os.rename(old_filepath, new_filepath)
Use regular expression, particularly re.sub:
>>> import re
>>> filename = '12name34with56numbers78in9it.txt'
>>> re.sub(r'\d', '', filename)
'namewithnumbersinit.txt'
This replaces everything that matches the \d pattern, i.e. that is a number, with '', i.e. nothing.
If you want to protect the extension, it get's more messy. You have to split the extension from the string, replace numbers in the first part, then join the extension back on. os.path.splitext can help you with that:
>>> filename = '12name34with56numbers78in9it.mp3'
>>> name, ext = os.path.splitext(filename)
>>> re.sub(r'\d+', '', name) + ext
'namewithnumbersinit.mp3'
You can do this:
filename = "this2has8numbers323in5it"
filename = "".join(char for char in filename if not char.isdigit())
No imports necessary.
import os
def rename_files():
# Get files names from the directory
files_names = os.listdir(" file r directory path")
saved_dir = os.chdir(files_name)
# To get cutrrent working directiry name
print os.getcwd() # This is to verify whether you are in correct path
for file_name in files_names:
os.rename(file_name,file_name.translate(None,'0123456789'))
# Above translate function remove all the number from file name
rename_files()
I need help.
there is a folder "C:\TEMP" in this folder are formatted files "IN_ + 7123456789.amr"
It is necessary to make renaming of files for a given pattern.
"IN_ NAME _ DATE-CREATE _ Phone number.amr"
Correspondingly, if a file called "OUT_ + 7123456789.amr" the result format "OUT_ NAME_DATE-CREATE_Phone number.amr"
The question is how to specify the file name has been checked before os.rename and depending on the file name to use the template
import os
path = "C:/TEMP"
for i, filename in enumerate(os.listdir(path)):
os.chdir(path)
os.rename(filename, 'name'+str(i) +'.txt')
i = i+1
Sorry but none of your examples are consistent in your question, I still don't understand what your C:\temp contains...
Well, assuming it would look like:
>>> os.listdir(path)
['IN_ + 7123456789.amr', 'OUT_ + 7123456789.amr']
The example:
import datetime
import re
import os
os.chdir(path)
for filename in os.listdir(path):
match = re.match(r'(IN|OUT)_ \+ (\d+).amr', filename)
if match:
file_date = datetime.datetime.fromtimestamp(os.stat(filename).st_mtime)
destination = '%s_%s_%s_Phone number.amr' % (
match.group(1), # either IN or OUT
match.group(2),
file_date.strftime('%Y%m%d%H%M%S'), # adjust the format at your convenience
)
os.rename(filename, destination)
Will produce:
IN_7123456789_20150721094227_Phone number.amr
OUT_7123456789_20150721094227_Phone number.amr
Other files won't match the re.match pattern and be ignored.
I've been trying to get this to work, but I feel like I'm missing something. There is a large collection of images in a folder that I need to rename just part of the filename. For example, I'm trying to rename the "RJ_200", "RJ_600", and "RJ_60"1 all to the same "RJ_500", while keeping the rest of the filename intact.
Image01.Food.RJ_200.jpg
Image02.Food.RJ_200.jpg
Image03.Basket.RJ_600.jpg
Image04.Basket.RJ_600.jpg
Image05.Cup.RJ_601.jpg
Image06.Cup.RJ_602.jpg
This is what I have so far, but it keeps just giving me the "else" instead of actually renaming any of them:
import os
import fnmatch
import sys
user_profile = os.environ['USERPROFILE']
dir = user_profile + "\Desktop" + "\Working"
print (os.listdir(dir))
for images in dir:
if images.endswith("RJ_***.jpg"):
os.rename("RJ_***.jpg", "RJ_500.jpg")
else:
print ("Arg!")
The Python string method endswith does not do pattern-matching with *, so you're looking for filenames which explicitly include the asterisk character and not finding any.
Try using regular expressions to match your filenames and then building your target filename explicitly:
import os
import re
patt = r'RJ_\d\d\d'
user_profile = os.environ['USERPROFILE']
path = os.path.join(user_profile, "Desktop", "Working")
image_files = os.listdir(path)
for filename in image_files:
flds = filename.split('.')
try:
frag = flds[2]
except IndexError:
continue
if re.match(patt, flds[2]):
from_name = os.path.join(path, filename)
to_name = '.'.join([flds[0], flds[1], 'RJ_500', 'jpg'])
os.rename(from_name, os.path.join(path, to_name))
Note that you need to do your matching with the file's basename and join on the rest of the path later.
You don't need to use .endswith. You can split the image file name up using .split and check the results. Since there are several suffix strings involved, I've put them all into a set for fast membership testing.
import os
import re
import sys
suffixes = {"RJ_200", "RJ_600", "RJ_601"}
new_suffix = "RJ_500"
user_profile = os.environ["USERPROFILE"]
dir = os.path.join(user_profile, "Desktop", "Working")
for image_name in os.listdir(dir):
pieces = image_name.split(".")
if pieces[2] in suffixes:
from_path = os.path.join(dir, image_name)
new_name = ".".join([pieces[0], pieces[1], new_suffix, pieces[3]])
to_path = os.path.join(dir, new_name)
print("renaming {} to {}".format(from_path, to_path))
os.rename(from_path, to_path)
I’m new to programming and could do with a little help. I’m trying to make a program that will search for files in a specified directory by extension (multiple extensions) and then only return specific results which have my list of keywords in the filename.
I have the following:
import os
from fnmatch import fnmatch
root = 'c:\users'
pattern = "*.css"
for path, subdirs, files in os.walk(root):
for name in files:
if fnmatch(name, pattern):
print os.path.join(name)
This will bring back all files with a single extension, in this case .css files, but I need it to do more such as image and text file extensions. I would also like it to only return files that have specific keywords in the file name. Can anyone point me in the right direction??
Thanks
Perhaps you could use glob:
from glob import glob
for filename in glob('*.css'):
print(filename)
If you have multiple extensions you can add the list returned by glob():
exts = ['ccs', 'txt']
all = []
for ext in exts:
all += glob('*.' + ext)
for filename in all:
print(filename)
Ok, so if you are searching for filetype and keywords, here would be some easy code to start with:
import os
import re
root = 'c:\users'
pattern = re.compile("((keyword1)|(keyword2))\.((txt)|(jpg))")
for path, subdirs, files in os.walk(root):
for name in files:
if re.match(pattern, name):
print os.path.join(name)
This code will match the file extension txt or jpg and search for the keywords keyword1 and keyword2.
If you want to make a more user-friendly code, to easily add extensions or keywords, you could use lists like so:
import os
import re
# configuration information
root = 'C:\Users'
keywords = [ 'one', 'two']
extensions = [ 'jpg', 'txt' ]
use_wildcard = True # Enables you to catch the keyword anywhere in the filename
# end of configuration
keyword_pattern = ''
first = True
for k in keywords:
if use_wildcard:
k = '.*' + k + '.*'
if first:
keyword_pattern += '(' + k + ')'
first = False
else:
keyword_pattern += '|(' + k + ')'
extension_pattern = ''
first = True
for ext in extensions:
if first:
extension_pattern += '(' + ext + ')'
first = False
else:
extension_pattern += '|(' + ext + ')'
pattern_regex = r"({0})\.({1})".format(keyword_pattern, extension_pattern)
print "Searching for: " + pattern_regex
pattern = re.compile(pattern_regex)
for path, subdirs, files in os.walk(root):
for name in files:
if re.match(pattern, name):
print os.path.join(path, name)
I'm using regular expressions because they are very powerful and are useful in a lot of cases. They may seem complex at first sight but once you understand them, it's difficult not to use them :)