0

I use google clould storage api. I get the filename like this 'pdf/randomPdf.pdf'

I use

new_filename = Path(file_name).stem + ".txt"

I do this so I can change the name of the extension to .txt

Now I want to change the 'pdf/...' to 'text/...'

How I can do it without split?

Iakovos Belonias
  • 1,217
  • 9
  • 25

3 Answers3

3

These answers 1 and 2 seem relevant to your question -

You can use os.path.splitext(filename) to extract everything but file extension - pdf/pdfFile in your case.

You can use os.path.dirname(filename) to extract the head - pdf in your case.

You can use os.path.basename(filename) to extract the tail - pdfFile.pdf in your case.

Abhineet Gupta
  • 624
  • 4
  • 12
2

Have you tried os.rename()?

Just do

os.rename("pdf/pdfFile.pdf", "text/pdfFile.txt")
Mike Yue
  • 21
  • 2
0

Use the string built-in method replace().

newFilename = filename.replace('pdf/', 'text/').replace('.pdf', '.txt')
John Gordon
  • 29,573
  • 7
  • 33
  • 58