Hello @jpeters,
welcome to the Plugin Café and thank you for reaching out to us. I have moved your question to General Talk since it is not related to our APIs. I would recommend having a look at our Forum Guidelines for future postings. Your question is also out of scope of support (as it is about the Python library/language). I will briefly answer here, but please understand that we cannot provide extended support on standard libraries or a language in general.
I cannot quite reproduce what you are reporting. It is true that Windows is quite locked down these days and prevents you from writing to multiple paths in the windows installation when you do not have admin rights. But the desktop is usually not among these paths (that would be a bit ridiculous).
I think the problem in your example is that you intend to do string formatting without actually doing it (you are missing the f-string prefix) and therefore attempt to write to the string literal C:\\Users\\{username}\\Desktop\\TurnTableTool
, which Windows then is going to prevent, as it won't let you create new directories in \Users\
without admin rights. {username}
should also be an illegal directory name.
If everything fails, you can still run Cinema 4D with admin rights, to propagate these admin rights to the Python VM. But as demonstrated by the example below, this should not be necessary.
Cheers,
Ferdinand
import c4d
import os
def main() -> None:
"""
"""
user = os.getlogin()
# You were missing the string fromating in your code (the f prefix), so you did
# not actually compose a new string.
composed = f"C:\\Users\\{user}\\Desktop"
print (composed)
# But I would do it differently anyways:
desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
print (desktop)
directoryPath = os.path.join(desktop, "TurnTableTool")
if not os.path.exists(directoryPath):
os.makedirs(directoryPath)
filePath = os.path.join(directoryPath, "myFile.txt")
with open(filePath, "w") as file:
file.write("Hello world!")
print (f"'{filePath}' does exist: {os.path.exists(filePath)}")
if __name__ == '__main__':
main()
C:\Users\f_hoppe\Desktop
C:\Users\f_hoppe\Desktop
'C:\Users\f_hoppe\Desktop\TurnTableTool\myFile.txt' does exist: True