cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

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...

weldermartins
Honored Contributor

TemplateHello 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')

5 REPLIES 5

Anonymous
Not applicable

Hey there! Thanks for your question. I'm sorry to hear you're having difficulty. Let's see what your peers have to say.

weldermartins
Honored Contributor

Similar to the result of this code, but to change files.

path = 'k3250.seqy0.company.csv'
 
path1 = path.replace("k3250", "company")
 
print(path1)

weldermartins
Honored Contributor

I managed to solve it using the code below.

thank's

if split[1]=='.COMPANYCSV':
        os.rename(files,files.replace("K3241", "COMPANY")) 

Anonymous
Not applicable

@welder martins​ - Thank you for sharing all this. You are fantastic!

Anonymous
Not applicable

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

Connect with Databricks Users in Your Area

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