Python: How to identify the root of the operating system?

Asked

Viewed 286 times

0

As I identify the root of the operating system (windows: "C: ", No linux: "/"), exclusively through python commands that apply to both systems?

  • If these are both values, why do you need a function for this? It would not be enough for one if checking if it’s Windows or Linux?

  • I need it captured from the operating system.

  • that question has already been answered: https://answall.com/questions/167296/howto detect the operating system

  • @Adrianogomes he asked to return the root folder of the only one that uses the application, not just identify the system

  • I got it, gentlemen. Thank you. Here’s the solution: https://answall.com/a/337395/62736

2 answers

3


Use the commands below:

>>> import os
>>> os.path.abspath(os.sep)
'/'

Results would be:

  • Linux returns "/"
  • Windows returns "C: " (or whatever the current disk is)
  • Works on Windows too?

  • @Andersoncarloswoss yes

  • I don’t think that’s what he asked for. What would be the result in windows?

  • @Sidon edited.

  • Okay, to be complete I would have to remove the scape, I mean, I would have to elaborate more, as I did in my reply. :-)

  • 1

    Escape is not part of the variable, it is not possible to "remove" it. It is only a visual representation, which appears when printing the repr() of the variable. The answer is correct.

  • Truth, things from the windows world, I think I’ve never done anything in python in windows. :-)

Show 2 more comments

0

I got the solution, gentlemen.

os.path.commonpath(os.get_exec_path())

The association of the above commands will always display the operating system root (OS), for any OS.

:D

  • If @sant0will’s answer works on any single, what is the advantage of your solution over his (more sleek and sleek)?

  • @Sidon, I’m not sure yet... I have to test, as it will have to work in network directories, and multiple drives... I will bring the result.

  • @Sidon, tested and works well.

  • 1

    @sant0will’s alternative is simpler, pythonica and works well!!! It is the best solution for this problem.

Browser other questions tagged

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