Problem: I have not been successful in simply creating a new .txt file onto my desktop.
I am truly at a loss. To be fair, I do not mess with files very much, so I am sure this is a simple solution. One weird thing that I have encountered is that it does create a folder instead of a file even though it has the file extension provided. From what I have researched though, this is very common.
I would not have asked this question if I could not already find it on this website, but I think my situation is a little unique.
I have tried multiple different methods including the following:
Code:
desktop_path = pathlib.Path.home() / 'OneDrive' / 'Desktop'
print (desktop_path)
def save_data():
file_name = last+'_'+first+'_'+'Symptom Checklist 90-R Results.txt'
file_path = Path(os.path.join(desktop_path, file_name))
table = tabulate(data, headers="firstrow", tablefmt="grid")
os.makedirs(file_path, exist_ok=True)
print(file_path)
try:
with open(file_path,'w') as file:
file.write(first_name+"\n"+last_name+"\n"+age+"\n"+birthday+"\n")
file.write(table)
file.close()
except PermissionError:
print("You do not have access to create this file.")
Here are what the print statements execute:
C:\Users\name\OneDrive\Desktop
C:\Users\name\OneDrive\Desktop\__Symptom Checklist 90-R Results.txt
You do not have access to create this file.
Again, I am sure this is a simple solution, and I am just blind to see it. I have also made sure that this file name doesn't already exist for whatever reason.
Thank you all for your insight and help. I ended up following this example found here: https://www.codeease.net/programming/python/how-to-run-python-script-as-admin
The first solution provided me with an easy solution. All I had to do was simply add this line: os.system(f'runas /user:Administrator "{file_path}"')