I have this small program and it needs to create a small .txt file in their ‘My Documents’ Folder. Here’s the code I have for that:
textfile=open('C:\Users\MYNAME\Documents','w')
lines=['stuff goes here']
textfile.writelines(lines)
textfile.close()
The problem is that if other people use it, how do I change the MYNAME to their account name?
Use
os.path.expanduser(path), see http://docs.python.org/library/os.path.htmle.g.
expanduser('~/filename')This works on both Unix and Windows, according to the docs.
Edit: forward slash due to Sven’s comment.