How to remove the OS user name

Asked

Viewed 52 times

3

I’m making a terminal and I have a question, how do I remove the user name behind the message?

exemplo: "C:/USERS/GUSTAVO/DESKTOP"

How do I leave:

example: "DESKTOP"

I’m using the OS library to get the directory.

Code:

import so
import sys
import platform
diretorio = os.path.dirname(os.path.abspath(__file__))
print(diretorio)

1 answer

3


Use the strip along with getuser

import os
import sys
import platform
from getpass import getuser
diretorio = os.path.dirname(os.path.abspath(__file__)).strip(getuser())
print(diretorio)
  • It didn’t work, buddy...

  • do you want to leave the last folder in the variable directory? I guess I didn’t get it right

  • Yes, as you can see in the example, there are other folders ( users,.

  • "C:/USERS/GUSTAVO/DESKTOP" this would look like "C:/USERS/DESKTOP"?

  • That’s right, I await answer <3

  • Solved, that code you updated, but you did not warn me solved poblema. Thanks! + 1

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.