我试图将一些数据写入%appdata%。一切似乎都像 Script1 的输出中所示的那样工作。正在创建新目录并保存文件,并且也成功检索数据。但尝试查看文件资源管理器中的数据时,该文件夹不存在! CMD也找不到文件和目录。
后来我手动创建了文件并检查,发生了什么。 CMD 现在可以找到该文件(我刚刚手动创建),但是当尝试使用 python 读取该文件时,它会向我输出 python 幽灵文件内容
test data 123
,而不是我刚刚写入其中的内容! (我还仔细检查了 WSL,新文件实际上包含 test data 456
。)
Script1(使用
test data 123
创建文件):
import os
import subprocess
appdata = os.getenv('APPDATA')
directory_path = f"{appdata}\\com-company\\prod-product-version3"
file_path = directory_path + "\\file1.txt"
print(f"Directories Exist: {os.path.exists(directory_path)}")
if not os.path.exists(directory_path):
os.makedirs(directory_path)
print("Directories created")
print(f"Directories Exist: {os.path.exists(directory_path)}")
print(f"File Exist: {os.path.exists(file_path)}")
print(f"Writing File: {file_path}")
with open(file_path, 'w')as fp:
fp.write("test data 123")
print(f"File Exist: {os.path.exists(file_path)}")
print(f"Reading File: {file_path}")
with open(file_path, 'r')as fp:
print(f"File Content: {fp.read()}")
print('---------------------')
cmd = f"dir {directory_path}"
try:
output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT, text=True)
print(output)
except subprocess.CalledProcessError as e:
print(f'Error: {e}')
print(f'Error message:\n{e.output}')
print('---------------------')
cmd = f"dir {file_path}"
try:
output = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT, text=True)
print(output)
except subprocess.CalledProcessError as e:
print(f'Error: {e}')
print(f'Error message:\n{e.output}')
输出:
Directories Exist: False
Directories created
Directories Exist: True
File Exist: False
Writing File: C:\Users\one\AppData\Roaming\com-company\prod-product-version3\file1.txt
File Exist: True
Reading File: C:\Users\one\AppData\Roaming\com-company\prod-product-version3\file1.txt
File Content: test data 123
---------------------
Error: Command 'dir C:\Users\one\AppData\Roaming\com-company\prod-product-version3' returned non-zero exit status 1.
Error message:
The system cannot find the file specified.
---------------------
Error: Command 'dir C:\Users\one\AppData\Roaming\com-company\prod-product-version3\file1.txt' returned non-zero exit status 1.
Error message:
The system cannot find the path specified.
手动创建
C:\Users\one\AppData\Roaming\com-company\prod-product-version3\file1.txt
并将数据写入其中:
test data 456
Script2(读取
test data 123
,即使它包含 test data 456
):
import os
appdata = os.getenv('APPDATA')
directory_path = f"{appdata}\\com-company\\prod-product-version3"
file_path = directory_path + "\\file1.txt"
print(f"File Exist: {os.path.exists(file_path)}")
print(f"Reading File: {file_path}")
with open(file_path, 'r')as fp:
print(f"File Content: {fp.read()}")
输出:
File Exist: True
Reading File: C:\Users\one\AppData\Roaming\com-company\prod-product-version3\file1.txt
File Content: test data 123
使用 WSL 进行双重检查:
cat /mnt/c/Users/one/AppData/Roaming/com-company/prod-product-version3/file1.txt
Output: test data 456
PS: 我重新启动了系统,python 仍然认为该文件包含
test data 123
。
正常情况下写作就可以了:
with open('C:\\Users\\one\\Desktop\\file2.txt', 'w') as fp:
fp.write('test data 789')
您可能遇到缓存问题。
尝试
refreshenv
,它应该刷新您用于构造文件路径的 APPDATA 变量。
python -m site --user-site
可以向您显示 python 存储站点包目录的位置,其中 python 可能会缓存文件/模块。
使用在较低级别上运行的函数可能会有所帮助,例如
os.scandir
在 Reddit 上找到答案。本质上,问题在于您从 Microsoft App Store 安装了 Python,并且它正在被沙箱化。卸载它并从 python.org 重新安装。