j wales
j wales

Reputation: 15

How can I get file's name from variable with its full path in python?

Let's say I have a = "C:\folder1\folder2\picture.jpg" How can I assign variable b to file's name with its extension: b = 'picture.jpg'

Upvotes: 1

Views: 69

Answers (1)

Miguel
Miguel

Reputation: 159

Without too many complications, you can just use the os module as follows:

import os
a = "C:\folder1\folder2\picture.jpg"
b = os.path.basename(a)

Which will output what you desire: 'picture.jpg'

Upvotes: 1

Related Questions