Reputation: 63
So I have a list of files from glob that are formated in the following way
filename xx xxx moretxt.txt
what I'am trying to do is rename them as follows
filename.txt
the first two xx is one of these:
[1B, 2B, 3B, 4B, 5B, 6B, 7B, 8B, 9B, 10B, 11B, 12B, 1A, 2A, 3A, 4A, 5A, 6A, 7A, 8A, 9A, 10A, 11A, 12A]
so how do I remove the "xx xxx moretxt" from the file name and keep the extension?
import glob, os
os.chdir("C:\\somepath")
for file in glob.glob("**/*.txt", recursive = True):
print(file)
Upvotes: 2
Views: 225
Reputation: 63
When I try this, I only get one filename..
import re
import glob, os
os.chdir("C:\\Somepath")
for filename in glob.glob("**/*.txt", recursive = True):
filename = re.match(r"(?P<filename>\w+).*\.(?P<ext>.+)", filename)
filename = "{}.{}".format(filename.group('filename'), filename.group('ext'))
print(filename)
output:
screw12323.txt
screw12323.txt
screw12323.txt
screw12323.txt
screw12323.txt '
Upvotes: 0
Reputation: 82815
Using str.split
Ex:
filename = "filename xx xxx moretxt.txt"
val = filename.split()
filename = "{}.{}".format(val[0], val[-1].split(".")[-1])
print(filename)
or using re.match
Ex:
import re
filename = "filename xx xxx moretxt.txt"
filename = re.match(r"(?P<filename>\w+).*\.(?P<ext>.+)", filename)
filename = "{}.{}".format(filename.group('filename'), filename.group('ext'))
print(filename)
Output:
filename.txt
Upvotes: 2