0

I have an application which should display to the user the path to a file. Let's say the user inputs /home/me/foo/, the application should shorten that to ~/foo.

Is there any way to do this in Python such that it works cross-platform with Linux and Windows?

1 Answer 1

1

now sure what you are trying to do but changing it to the other format is as simple as:

newpath = "~"+filepath[9:]

if you want this to work with other usernames,

newpath = "~/"+filepath[findfirstindex('foo'):]
Sign up to request clarification or add additional context in comments.

2 Comments

Will this work if the user has a much longer or shorter name?
changed my original answer. you can use findfirstindex to start wherever the first index occurs if there are other usernames

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.