How to Retrieve name of current Windows User (AD or local) using Python?
How can I retrieve the name of the currently logged in user, using a python script? The function should work regardless of whether it is a domain/ad user or a local user.
Asked by: Brooke888 | Posted: 28-01-2022
Answer 1
Try this:
import os;
print os.environ.get( "USERNAME" )
That should do the job.
Answered by: Agata832 | Posted: 01-03-2022Answer 2
as in https://stackoverflow.com/a/842096/611007 by Konstantin Tenzin
Look at getpass module
import getpass getpass.getuser()
Availability: Unix, Windows
Note "this function looks at the values of various environment variables to determine the user name. Therefore, this function should not be relied on for access control purposes (or possibly any other purpose, since it allows any user to impersonate any other)."
at least, definitely preferable over getenv
.
Answer 3
win32api.GetUserName()
win32api.GetUserNameEx(...)
See: http://timgolden.me.uk/python/win32_how_do_i/get-the-owner-of-a-file.html
Answered by: Anna135 | Posted: 01-03-2022Answer 4
I don't know Python, but for windows the underlying api is GetUserNameEx, I assume you can call that in Python if os.environ.get( "USERNAME" ) doesn't tell you everything you need to know.
Answered by: Miller667 | Posted: 01-03-2022Answer 5
Pretty old question but to refresh the answer to the original question "How can I retrieve the name of the currently logged in user, using a python script?" use:
import os
print (os.getlogin())
Per Python documentation: getlogin - Return the name of the user logged in on the controlling terminal of the process.
Answered by: Wilson570 | Posted: 01-03-2022Similar questions
python - How to retrieve a directory of files from a remote server?
If I have a directory on a remote web server that allows directory browsing, how would I go about to fetch all those files listed there from my other web server? I know I can use urllib2.urlopen to fetch individual files, but how would I get a list of all the files in that remote directory?
python - Retrieve multiple files from same directory
I am creating a GUI program that has 25 buttons, each accesses a unique file in the same folder, I know I can individually program each one to go to each file, but that seems like a lot more code than there needs to be, is there a more efficient way to do this?
here is the code:
box1 = 'C:/Users/Geekman2/Documents/Tests/box1.txt
cupcake = Button(donut,text = "Box #1", command = open(box1))
How do I retrieve every file with a HTTP 200 response code in a certain directory with Python
I'm trying to retrieve every PDF file of the Universal Declaration of Human Rights, which is found in English here.
As you can see, the file path includes ".../UDHR_Translations/eng.pdf". I want to download every version of the PDF file, a-z, that returns an HTTP 200 response code. Without checking for the...
python - How to retrieve the files under a website directory which forbids access
i need to print all the files under a website's directory.
The directory of the website is :
http://screenshots.ubuntu.com/screenshots/e/eclipse/
This is the path of the file which is under the directory :
http://screenshots.ubuntu.com/screenshots/e/eclipse/9128_large.png
For doing it, i write this code :
import os
import urllib
url = "...
python - Retrieve files from only the third level of directory
I have a directory which has the following form :
A/ : the root
B/: The first level subdirectory which contains the following directories
01/ 02/ 03/ 04/ 05/ 06/ 07/
C/: third leve where each subdirectory from B/ (01/ or 02/ or 03/ or 04/ or 05/ or 06/ or 07/)
contains up to three subdirectories
001/ 002/ 003/
It's at 001/ 002/ 003/ that l want to retr...
python - Using Azure Key Vault and Active Directory to Retrieve Secrets
For a Python code base I would like to have developers accessing application secrets using Azure Key Vault, with the idea that when we deploy, the application also should be able to connect. Hence, I'm thinking Active Directory.
However, I can not find any examples on the interweb that show this with the Python SDK. Initially, I would think to retrieve the CLI user:
from azure.common.credentials imp...
How do I retrieve all directory paths from Azure Data Lake Storage Gen2 using Python?
I'm trying to retrive all paths to directories in Azure Data Lake Storage Gen2 using the approach mentioned here. Here's my code:
# Connect to account
def initialize_storage_account_ad(storage_account_name, client_id, client_secret, tenant_id):
try:
globa...
Python | choose a file in directory and retrieve its pathname
I'm interested in using file dialogs to choose the files I want to use for analysis in python versus just copying and pasting the pathname into a variable. With keeping user experience in mind, most people are used to navigating their files through the file dialog box on windows or mac.
I'd like to replicate this experience here. The below code will utilize ipywidgets in an .ipynb file to allow the user to choose ...
ftp - Using Python's ftplib to get a directory listing, portably
You can use ftplib for full FTP support in Python. However the preferred way of getting a directory listing is:
# File: ftplib-example-1.py
import ftplib
ftp = ftplib.FTP("www.python.org")
ftp.login("anonymous", "ftplib-example-1")
data = []
ftp.dir(data.append)
ftp.quit()
for line in data:
print "-", line
Which yields:
$ python ftplib-example-1.py
- total 34
- dr...
How do I change directory back to my original working directory with Python?
I have a function that resembles the one below. I'm not sure how to use the os module to get back to my original working directory at the conclusion of the jar's execution.
def run():
owd = os.getcwd()
#first change dir to build_dir path
os.chdir(testDir)
#run jar from test directory
os.system(cmd)
#change dir back to original working directory (owd)
note: I think...
python - How to check if a file can be created inside given directory on MS XP/Vista?
I have a code that creates file(s) in user-specified directory. User can point to a directory in which he can't create files, but he can rename it.
I have created directory for test purposes, let's call it C:\foo.
I have following permissions to C:\foo:
Traversing directory/Execute file
Removing subfolders and files
Removing
Read permi...
java - Mixed language source directory layout
We are running a large project with several different languages: Java, Python, PHP, SQL and Perl.
Until now people have been working in their own private repositories, but now we want to merge the entire project in a single repository. The question now is: how should the directory structure look? Should we have separate directories for each language, or should we separate it by component/project? How well does pyt...
import - Current working directory no longer inherited from calling process from python 2.5 onwards?
I updated my python version on windows 2003 server from 2.4 to 2.5.
In 2.4 I could import a file "sub1.py" from a subdirectory c:\application\subdir\ like this:
import sub1
as long as the calling script main.py that lives in c:\application was started like this:
c:\application\subdir>python ..\main.py
But in 2.5 it no longer works for me:
How do I tell a file from directory in Python?
When using os.listdir method I need to tell which item in the resulting list is a directory or just a file.
I've faced a problem when I had to go through all the directories in this list, and then add a file in every single directory.
Is there a way to go through this list and remove all files from it? If it isn't possible to do with os.listdir, what method should I use instead?
Thanks.
python - Using paver and nose together with an atypical directory structure
I'm trying to write a task for Paver that will run nosetests on my files.
My directory structure looks like this:
project/
file1.py
file2.py
file3.py
build/
pavement.py
subproject/
file4.py
test/
file5.py
file6.py
Doctests (using the --with_doctest option) should be run on all the *.py files, while onl...
file - Directory checksum with python?
So I'm in the middle of web-based filesystem abstraction layer development.
Just like file browser, except it has some extra features like freaky permissions etc.
I would like users to be notified somehow about directory changes.
So, i.e. when someone uploads a new file via FTP, certain users should get a proper message. It is not required for the message to be extra detailed, I don't really need to show the exa...
windows - Changing prompt working directory via Python script
Is it possible to change the Windows command prompt working directory via Python script?
e.g.
>> cd
>> c:\windows\system32
>> make_decision_change_dir.py
>> cd
>> c:\windows
I have tried a few things which don't work:
import os
os.chdir(path)
import os, subprocess
subprocess.Popen("chdir /D \"%s\"" %path,...
Directory Walker for Python
I am currently using the directory walker from Here
import os
class DirectoryWalker:
# a forward iterator that traverses a directory tree
def __init__(self, directory):
self.stack = [directory]
self.files = []
self.index = 0
def __getitem__(self, index):
while 1:
try:
file = self.file...
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python