Liam Chasteen Liam Chasteen 1, 1 1 gold badge 10 10 silver badges 2 2 bronze badges. Alternatively slightly nicer to read : pwd. What does it do on Windows? This is the correct way if you need to get the username both with and without logging in. This method is on unix-like systems much superior to Konstantin Tenzin's answer, because it handles the sudo case correctly. I am aware the OP didn';t ask for unix-like ssytems. Show 1 more comment.
You can also use: os. Marcin Augustyniak Marcin Augustyniak 1, 1 1 gold badge 7 7 silver badges 2 2 bronze badges. On linux, getlogin returns the name of the "user logged in on the controlling terminal of the process.
If you used su, then this won't return the current user, but the originally logged in user. Python 3. It's only available on Windows for Python 3. Thanks for this. The login user is not exactly what the poster asked for but it is what I was looking for.
Show 3 more comments. You can probably use: os. Aaron McDaid Nadia Alramli Nadia Alramli k 35 35 gold badges silver badges bronze badges. MikeGraham os. GetUserName win32api. Adam Adam 3 3 silver badges 3 3 bronze badges. This answer is at least as useful as the useless vote upvoted unix-only answer. Presumably the right answer is a combination of the two. If you're stuck on python 2 on Windows, this is the only safe way to do it. HezniK HezniK 2 2 silver badges 2 2 bronze badges. Someone's home directory does not always reflect their username.
This solution is clever, but makes some assumptions that aren't always true. There is no constraint that requires the username to appear in the homedir path on Linux. It just happens to be the case most of the time, but a sysadmin can set a user's homedir to whatever they want. Import getpass get your username username getpassgetuser create your string string ospathjoin Documents and SettingsusernameDesktop with open usernametxt as file.
Python Server Side Programming Programming To get the homedir in python you can use ospathexpanduser from the os module. Add import os in code and then use by. Fredrik Heres one way to do it. How to find the real user home directory using Python. Youll learn how to do this using Windows Mac and Linux. Getting all window titles. First install psutil with pip Python 2 or pip3 Python 3. It is available in both. In this tutorial youll learn how to use Python to get a filename from a path for a given file.
I am trying to find a way to get the name of the user logged into windows into a Python Script. Bookmark this question. Paths consist of three main parts. Posix Name of the OS system. This post shows how you can retrieve all window titles in Microsoft Windows using Pythons ctypes module. I know that using getpass. So i want python to find the username and then implement it in the following script itself. I had success with os. Install win32com , then:. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to make Python get the username in windows and then implement it in a script Ask Question. Asked 9 years, 1 month ago.
Active 6 months ago. Viewed k times. Script: os. Improve this question. KingMak KingMak 1, 2 2 gold badges 12 12 silver badges 25 25 bronze badges. Are you just asking how to put the result of getpass. Gabriel Luci Gabriel Luci Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.
Podcast Helping communities build their own LTE networks.
0コメント