5

Hello how can I get the file name with os.path lib? For example:

C:\Users\filippo\Desktop\K.java 

I want the K without the extension file

1

2 Answers 2

6

I suggest you use the splitext and basename functions from os.path

K, ext = os.path.splitext(os.path.basename(my_path)) 

See the docs here.

Sign up to request clarification or add additional context in comments.

Comments

4

You can achieve this using:

import os filename = r"C:\Users\filippo\Desktop\K.java" print os.path.splitext(filename)[0] > C:\Users\filippo\Desktop\K print os.path.splitext(filename)[1] > .java K, ext = os.path.splitext(os.path.basename(filename)) print K print ext > K > .java 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.