Reputation:
I'm trying to write only a file name to a .txt
file, however, the entire file path is being written i.e. C:\Users\OneDrive\a.png
I only want a.png
to be written. I know I can slice a string in python like this:
a="abcdefg"
print(a[2:4])
cd
but how do I do this at a fixed point in a string?
The file path will always remain static although the file name and length itself will change.
Upvotes: 0
Views: 174
Reputation: 12701
you can split with \
and take the last element:
your_path = "C:\\Users\\OneDrive\\a.png"
print(your_path.split("\\")[-1])
Upvotes: 1