0

My problem is, imagine I need to make a python script which will create a text file in the user's C: drive when he runs the script.

The code I have used is,

    import os
    import getpass
    
    #taking the username
    uName = getpass.getuser()

    #defining the file path and name
    pathPy = "C:/Users/"+uName+"/death.txt"

    #creating the .txt file in the said path
    file1 = open(pathPy, 'w')
    file1.write("Hellow Death")
    file1.close()

This works on my PC as I am the admin. But when I tried running this on a VirtualBox, it gave an error 'Permission Denied'.

Can this be solved by making an .exe from this script which would request permission at the beginning? If so, please guide me through it!! I'm no pro at python so I would be grateful if someone could explain what happens along with the code. :)

Thank you in advance. <3

Update: I made an .exe of the python script and when run as the Admin, it does the job. But if I just double click on it, it does not work because I don't know how to make the script require admin privilege when trying to install. If someone can shred some light on it, that would be amazing! :)

  • This may be a repeat of this question. I'd give this a try https://stackoverflow.com/questions/56974927/permission-denied-trying-to-run-python-on-windows-10/57447610 – Adam Carr Oct 09 '20 at 11:44
  • Also what tool are you using to execute your script? git-bash? – Adam Carr Oct 09 '20 at 11:47
  • A user's profile directory is `os.path.expanduser('~')`, which in general can differ from the user name in "C:\Users". But creating a file in the profile directory is kind of rude. If the file should be conveniently accessible to the user, create it in the user's "Documents" or "Desktop" directory. (Use a library, ctypes, or winreg to query the path of special folders; don't rely on the default location.) Or simply ask the user where to create the file, e.g. via [`tkinter.filedialog.askdirectory`](https://docs.python.org/3/library/dialog.html#tkinter.filedialog.askdirectory). – Eryk Sun Oct 09 '20 at 14:59
  • @AdamCarr Thank You I will refer it! :) Actually I want to make an .exe file which the user will execute himself. I am currently using pyinstaller to make .exe from python scripts. – HunkyLunaticV Oct 09 '20 at 17:02
  • @ErykSun Actually what I need the file to be is out of the user's way. Also, I have tried creating the file in the Video folder but when I try to access the file using a .bat it does not run. Again, permission denied! :( – HunkyLunaticV Oct 09 '20 at 17:06
  • Creating a file in the current user's profile directory or any other of the user's shell folders (e.g. Videos) does not require administrator access. Something else is happening. You need to explain in detail how Python is installed (exact version and installation options) and how you're running the script. – Eryk Sun Oct 11 '20 at 01:01

1 Answers1

2

If anyone came across the same issue, I figured out if I made the exe require admin privileges when installing, it does the job. How to do it:

I used pyinstaller to make the exe from the python script.

    pyinstaller scriptname.py --onefile --uac-admin

This way, at the installation, it prompts for admin rights. Hence does the job! :)