Reputation:
I know that using getpass.getuser()
command, I can get the username, but how can I save it in a text file ? So I want python to find the username and then save it in the following file username.txt
Script:
os.path.join('..','Documents and Settings','USERNAME','Desktop'))
(Python Version 2.7 being used)
Upvotes: 0
Views: 2162
Reputation: 187
You have to open a txt file, construct your string then write to it.
import getpass
# get your username
username = getpass.getuser()
# open a txt file in append mode
log = open('username.txt', 'a')
# create your string
string = os.path.join('..','Documents and Settings',username,'Desktop')
# save and close the file
log.write(string)
log.close()
or you could use pythons 'with' statement which makes sure the file is closed properly.
import getpass
# get your username
username = getpass.getuser()
# create your string
string = os.path.join('..','Documents and Settings',username,'Desktop')
with open('username.txt') as file:
file.write(string)
Upvotes: 2