0

I am looking to create a log file on a users desktop, but since I do not know the username of the person using the computer I cant give my program the directory of where to place the file.

This is what works on PC since my username is mikur.

file = open("C:\\Users\\Mihkel\\Desktop\\KeyLog.txt", 'a')

I have tried using

file = open("C:\\Users\\Public\\Desktop\\KeyLog.txt", 'a')

but that gives me a no permissions error. Is there a solution to this?

Mihkel
  • 689
  • 7
  • 34

1 Answers1

1

Try os.path.expanduser("~/Desktop")

cds84
  • 343
  • 1
  • 14
  • do I use this as the path/directory? – Mihkel Oct 02 '18 at 10:28
  • file = open(os.path.expanduser("~/Desktop/KeyLog.txt"), 'a') – cds84 Oct 02 '18 at 10:31
  • returns this error: NameError: name 'os' is not defined – Mihkel Oct 02 '18 at 10:38
  • you need to import os at the top of your source file. – cds84 Oct 02 '18 at 11:51
  • How do I access the file later, I need to send it back to me using MIME. It needs the directory and filename. what would go here: `filename='?????'` – Mihkel Oct 02 '18 at 17:50
  • Could you rephase the question? I fear we are going in circles... filename = os.path.expanduser("~/Desktop/KeyLog.txt") or, perhaps you need... filename = "KeyLog.txt" directory = os.path.expanduser("~/Desktop/") full_path = os.path.join(directory, filename) – cds84 Oct 04 '18 at 08:13
  • I figured it out myself, I used: filename=fullpath=os.path.abspath(os.path.expanduser("~/Desktop/Log.txt")) Thanks for the help though! – Mihkel Oct 04 '18 at 09:28