Hello everyone, I have a directory with 40 files. File names are divided into prefixes. I need to rename the prefix k3241 according to the name in the...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ02-04-2022 04:55 AM
Hello everyone, I have a directory with 40 files.
File names are divided into prefixes. I need to rename the prefix k3241 according to the name in the last prefix.
I even managed to insert the csv extension at the end of the file. but renaming files based on various prefixes is the tricky one. Can anybody help me. I will be posting the image of the code that was created.
Thanks.
/*
This code inserts the csv extension in the filename.
*/
path ="/dbfs/tmp/data/dadosjuridicos/descompactados"
for i in os.listdir(path):
files = os.path.join(path,i)
split= os.path.splitext(files)
if split[1]=='.COMPANY':
os.rename(files,split[0]+'COMPANY.csv')
if split[1]=='.ESTABLISHMENT':
os.rename(files,split[0]+'ESTABLISHMENT.csv')
if split[1]=='.PARTNER':
os.rename(files,split[0]+'PARTNER.csv')

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ02-04-2022 11:21 AM
Hey there! Thanks for your question. I'm sorry to hear you're having difficulty. Let's see what your peers have to say.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ02-08-2022 05:05 AM
Similar to the result of this code, but to change files.
path = 'k3250.seqy0.company.csv'
path1 = path.replace("k3250", "company")
print(path1)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ02-08-2022 10:31 AM
I managed to solve it using the code below.
thank's
if split[1]=='.COMPANYCSV':
os.rename(files,files.replace("K3241", "COMPANY"))

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ02-09-2022 08:10 AM
@welder martinsโ - Thank you for sharing all this. You are fantastic!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ04-13-2022 07:39 AM
Hi @welder martinsโ
How are you doing?
Thank you for posting that question. We are glad you could resolve the issue. Would you want to mark an answer as the best solution?
Cheers

