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')
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.
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)
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"))
02-09-2022 08:10 AM
@welder martins - Thank you for sharing all this. You are fantastic!
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
Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.
If there isn’t a group near you, start one and help create a community that brings people together.
Request a New Group