3

I need to check if a user exists in either Linux or Windows, using Python 3 (3.7 right now for reasons unrelated).

No, pwd doesn't work on Windows.

No, getpass only returns current username, while I need to check any username.

martineau
  • 119,623
  • 25
  • 170
  • 301
LetMeSOThat4U
  • 6,470
  • 10
  • 53
  • 93
  • for linux, check if `/home/username` folder exist. For Windows check if `c:\Users\username` exist. You can know if you are on windows or linux using `platform.system()`, check https://stackoverflow.com/questions/110362/how-can-i-find-the-current-os-in-python –  Jul 22 '22 at 13:31
  • User management is inherently OS-specific, what makes you think there is a portable solution? – Ulrich Eckhardt Jul 22 '22 at 13:55
  • Does this answer your question? [How to get all Windows/Linux user and not only current user with python](https://stackoverflow.com/questions/56300490/how-to-get-all-windows-linux-user-and-not-only-current-user-with-python) – Richard Neumann Jul 22 '22 at 17:23

1 Answers1

-1

as far as i know, there is no universal command for both distros, but you can check the env in your script

import os
if os.name [...]

or

import platform
my_os = platform.system()

based on this you can create different functions for both os.

  • this doesn't answer the question. It doesn;t check if a certain user exist –  Jul 22 '22 at 13:35
  • 1
    only if you don't understand.. I need to check if a user exists in either Linux or Windows, using Python 3 (3.7 right now for reasons unrelated). No, pwd doesn't work on Windows. No, getpass only returns current username, while I need to check any username. So he was going to use a universal method which doesn't exist. For windows you can go with : Get-LocalUser and on linux awk -F: '{ print $1}' /etc/passwd or less /etc/passwd – y0sh1da Jul 22 '22 at 13:50
  • @yosh1da Why not transform your comment into full blown answer? I'd accept it. – LetMeSOThat4U Apr 05 '23 at 10:16