Peaser
Peaser

Reputation: 575

How can I access a file in a completely different directory based on absolute path (Python)

import os
test = os.path.exists("c:/conf.txt")
if test == False:
    with open("c:/conf.txt", "w") as Inc:
        Inc.write("0")
        Inc.close()
        quit()
if test == True:
    f = open("c:/conf.txt", 'r')
    b = int(f.readline())
    b +=1
    with open("c:/conf.txt", 'w') as writeinc:
        writeinc.write(str(b))
        writeinc.close()

using open(c:/conf.txt) doesn't work (also tried c:\.)

I get the following error message:

IOError: [Errno 22] invalid mode ('w') or filename: 'c:/conf.txt'

Is there a way to access a different directory using open() according to absolute path rather than relative?

Upvotes: 0

Views: 95

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59681

Sounds to me like you don't have permission to write to the root path of your drive. In Windows 7 and 8, you cannot create files in the root directory:

In Windows 7 or 8 (may be Vista), users (even administrators) are not allowed to create files in the C drive root directory, otherwise, an error message like “A required privilege is not held by the client” or “access is denied” will be prompted.

Source

The article goes on how to modify the registry if you want to get around this restriction.

  1. Press keys “Windows Key + R”, type regedit
  2. Locate HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\EnableLUA
  3. Update the EnableLUA value to 0 (turn if off)
  4. Restart Windows.

Upvotes: 1

Related Questions