1

In a Python3 program running in Windows 10, os.system() is not able to find a file created in the APPDATA directory using with open, even after that same file can be successfully read by a subsequent with open.

QUESTION FOR THIS OP:

What specific syntax needs to be changed in the Python3 code below in order to successfully create a file in the APPDATA directory that can be found visually by users of the Windows 10 Explorer UI and also programmatically by the os.system() command below which is failing?

CURRENT CODE:

The following code is what we are currently trying:

print(os.getenv('APPDATA'))
terraformRC = os.path.join( os.getenv('APPDATA'), "terraform.rc")
print("terraformRC is: ", terraformRC)
print("About to write terraformRC. ")
try: 
  with open(terraformRC, 'w') as f:
    f.write('provider_installation {\n')
    f.write('  filesystem_mirror {\n')
    f.write('    path    = "' + providersPath + '"\n')
    f.write('    include = ["*/*"]\n')
    f.write('  }\n')
    f.write('\n')
    f.write('  direct {\n')
    f.write('    exclude = ["*/*"]\n')
    f.write('  }\n')
    f.write('}\n')
except (Exception) as e:
  print(e)

print("About to read the terraformRC we just wrote.  ")
with open(terraformRC, 'r') as lines:
  for line in lines:
    print(line)

print("About to disable settings for folder so that the terraformRC file can be unhidden.  ")
removeSettingsCmd = 'attrib -h -s ' + terraformRC
os.system(removeSettingsCmd)

CURRENT FAILED RESULTS:

The following output is printed by Windows CMD when we call the above Python3 code from Windows CMD.

C:\path\to\AppData\Roaming
terraformRC is:  C:\path\to\AppData\Roaming\terraform.rc
About to write terraformRC.
About to read the terraformRC we just wrote.
provider_installation {
  filesystem_mirror {
    path    = "C:\path\to\dependencies\terraform\providers"
    include = ["*/*"]
  }
  direct {
    exclude = ["*/*"]
  }
}

About to disable settings for folder so that the terraformRC file can be unhidden.
File not found - C:\path\to\AppData\Roaming\terraform.rc

THE PROBLEM:

As you can see from the output above, Python seems to successfully find the APPDATA directory. Then Python seems to successfully write a terraformRC file to APPDATA. Then Python seems to successfully read the terraformRC file that it just seems to have written to APPDATA.

The problem is that the os.system(removeSettingsCmd) then fails with a message File not found - C:\path\to\AppData\Roaming\terraform.rc stating that it cannot find the terraformRC file at the correct location. And also, a human user is not able to view the terraformRC file when looking for it in APPDATA using Windows Explorer.

23
  • @lit: No, that's not relevant. Commented Aug 9, 2021 at 23:04
  • What happens if you try to manually enter the attrib command for the file on the command line? Commented Aug 9, 2021 at 23:07
  • 1
    I think you may need quotes around the filename: removeSettingsCmd = 'attrib -h -s "' + terraformRC + '"' Commented Aug 9, 2021 at 23:11
  • 1
    @CodeMed BTW, "a human user is not able to view the terraformRC file when looking for it in APPDATA using Windows Explorer" isn't a useful data point given that the file (somehow) started off with the hidden and/or system attributes. (Why does it have those attributes at all?) Commented Aug 9, 2021 at 23:51
  • 1
    @CodeMed that’s not what I was referring to. print('a ',b) will adds a space between the two items, which is why your prints have two spaces, and explains what @janesdlin pointed out Commented Aug 10, 2021 at 6:05

1 Answer 1

3

It appears you have installed Python from the Microsoft Store. Store apps are Universal Windows Platform (UWP) apps and redirect AppData storage on a per-user basis to a unique user-specific area that a native app such as attrib is unaware of. For more information see UWP - store and retrieve settings and other app data.

I recommend uninstalling the UWP version of Python and installing an official native binary from python.org, which will work as you expect.

Sign up to request clarification or add additional context in comments.

1 Comment

This solves the problem that was described in the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.