Später habe ich die Datei manuell erstellt und überprüft, was passiert ist. Das CMD konnte jetzt die Datei finden (die ich gerade manuell erstellt habe), aber als ich versuchte, die Datei mit Python zu lesen, gab es mir die Testdaten 123 zum Inhalt der Python-Ghost-Datei aus und nicht das, was ich gerade hineingeschrieben hatte! Ich habe auch mit WSL überprüft, ob die neue Datei tatsächlich Testdaten 456 enthält.
Warum passiert das und wie kann ich es beheben?
Skript1 (Erstellen der Datei mit Testdaten 123):
Code: Select all
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}')
Code: Select all
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.
Code: Select all
test data 456
Code: Select all
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()}")
Code: Select all
File Exist: True
Reading File: C:\Users\one\AppData\Roaming\com-company\prod-product-version3\file1.txt
File Content: test data 123
Code: Select all
cat /mnt/c/Users/one/AppData/Roaming/com-company/prod-product-version3/file1.txt
Output: test data 456
Ich habe mein System neu gestartet und Python glaubt immer noch, dass die Datei Testdaten 123 enthält.
Und das Schreiben funktioniert normalerweise einwandfrei:
Code: Select all
with open('C:\\Users\\one\\Desktop\\file2.txt', 'w') as fp:
fp.write('test data 789')
Mobile version