`os.system` 无法找到由 `with open` 创建并由 `with open` 读取的文件
`os.system` not able to find file created by `with open` and read by `with open`
在 Python3 程序 运行 中 Windows 10,os.system()
无法找到在 APPDATA
目录中使用 with open
,即使同一文件可以被后续 with open
.
成功读取
此 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?
当前代码:
下面的代码是我们目前正在尝试的:
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)
当前失败的结果:
当我们从 Windows CMD 调用上述 Python3 代码时,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
问题:
从上面的输出可以看出,Python似乎成功找到了APPDATA
目录。然后 Python 似乎成功将 terraformRC 文件写入 APPDATA
。然后 Python 似乎成功读取了它似乎刚刚写入 APPDATA
.
的 terraformRC 文件
问题是 os.system(removeSettingsCmd)
然后失败并显示一条消息 File not found - C:\path\to\AppData\Roaming\terraform.rc
,指出它无法在正确的位置找到 terraformRC 文件。而且,当使用 Windows Explorer 在 APPDATA
中查找 terraformRC 文件时,人类用户无法查看它。
您似乎已经从 Microsoft Store 安装了 Python。商店应用程序是通用 Windows 平台 (UWP) 应用程序,并基于每个用户将 AppData 存储重定向到 attrib
等本机应用程序不知道的唯一用户特定区域。有关详细信息,请参阅 UWP - store and retrieve settings and other app data。
我建议卸载 Python 的 UWP 版本并安装来自 python.org 的官方本机二进制文件,这将如您所愿。
在 Python3 程序 运行 中 Windows 10,os.system()
无法找到在 APPDATA
目录中使用 with open
,即使同一文件可以被后续 with open
.
此 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 theos.system()
command below which is failing?
当前代码:
下面的代码是我们目前正在尝试的:
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)
当前失败的结果:
当我们从 Windows CMD 调用上述 Python3 代码时,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
问题:
从上面的输出可以看出,Python似乎成功找到了APPDATA
目录。然后 Python 似乎成功将 terraformRC 文件写入 APPDATA
。然后 Python 似乎成功读取了它似乎刚刚写入 APPDATA
.
问题是 os.system(removeSettingsCmd)
然后失败并显示一条消息 File not found - C:\path\to\AppData\Roaming\terraform.rc
,指出它无法在正确的位置找到 terraformRC 文件。而且,当使用 Windows Explorer 在 APPDATA
中查找 terraformRC 文件时,人类用户无法查看它。
您似乎已经从 Microsoft Store 安装了 Python。商店应用程序是通用 Windows 平台 (UWP) 应用程序,并基于每个用户将 AppData 存储重定向到 attrib
等本机应用程序不知道的唯一用户特定区域。有关详细信息,请参阅 UWP - store and retrieve settings and other app data。
我建议卸载 Python 的 UWP 版本并安装来自 python.org 的官方本机二进制文件,这将如您所愿。