0

I'm trying to write a program that allows user to open notepad as one of its functions. However, when trying to open an already-made text file, I got error:

[File path]

You do not have permission to open this file. See the owner of the file or an administrator to obtain permission.

The code I'm using is this:

    if not os.path.exists(location):
        os.makedirs(location)
    os.system("start notepad " + location)

I have created a text file if it doesn't already exist notepad would ask anyways, so I just want to take a step out for the user.

I know the location exists because when I checked it, the file I wish to create existed there, so I know the issue is with the os.system() command.

I found this: Failing to open a file using os.system(), as stack recommended it to me while writing this question, but it doesn't really answer my question.

Edit 1: I'm one windows 10.

Edit 2: I need to be able to run this piece of code from another python file, so I cannot run this script as an administrator in command line.

Edit 3: This question was marked as a duplicate to How to run python script with elevated privilege on windows, but even if I run my script as ADMIN (right clicking it, selecting "Run as administrator", it still brings up that error.

The file location is: E:\System Files\Logs\Logs[Date].txt, where date is the current date formatted in yyyy-mm-dd

TobyTobyo
  • 405
  • 2
  • 6
  • 20
  • would you try `sudo python yourProgram` ? – James Maa Sep 18 '17 at 03:40
  • @JamesMaa I'm on windows 10, and its saying "sudo is not recognized" – TobyTobyo Sep 18 '17 at 03:42
  • The `runas` command would be the replacement in `windows`, see the reference here https://technet.microsoft.com/en-us/library/bb490994.aspx – James Maa Sep 18 '17 at 03:44
  • @JamesMaa I'm not sure if I'm doing it right, its pulling up a help dialogue. Additionally, I'd like to be able to call this from another .py file (my program is a collection of files, makes it easier to fix things when a login procedure or something breaks). – TobyTobyo Sep 18 '17 at 03:49
  • Try running the command line as administrator – Santiago Benoit Sep 18 '17 at 03:55
  • What is `location`? Did you use forward slashes or backslashes? – user2357112 Sep 18 '17 at 04:07
  • @user2357112 location uses forwards slashes. It's a location that the user could enter, or that is generated from a base directory (drive number), and extended file location. – TobyTobyo Sep 18 '17 at 17:02
  • I'm pretty sure Notepad's command-line interface needs backslashes; forward slashes represent options, not directories. – user2357112 Sep 18 '17 at 17:04
  • @user2357112 backslashes don't work either. Here's what location is: E:\System Files\Logs\Logs – TobyTobyo Sep 18 '17 at 17:11
  • @user2357112 I got it to work, but it won't let me post an answer. It turned out to be an error with how I created the file if it didn't previously exist. – TobyTobyo Sep 18 '17 at 17:26
  • @cᴏʟᴅsᴘᴇᴇᴅ would you mind opening this post again, so I can post the correct answer in case anyone else would like to see the correct way to handle this? – TobyTobyo Sep 18 '17 at 17:34

0 Answers0