SOLVED Python writing to desktop

Hi there,

I've got a question, in my plugin I'm trying to create a directory on the Windows users' desktop to write files to with the following code:

username = os.getlogin()    #Get the windows user name

SavePathPackage = 'C:\\Users\\{username}\\Desktop\\TurnTableTool\\'

# -----Checking if directories exist, if not create them-----#
if (os.path.exists(SavePathPackage)):
    print("Path found on desktop")
else:
    os.makedirs("C:\\Users\\{username}\\Desktop\\TurnTableTool")
    print("Created path on desktop")

However, this throws en error:
“winerror 5 access is denied”

The usual fix is to run the code as admin but since this is a plugin within Cinema, that is not possible.

Any pointers perhaps?

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

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

Hi Ferdinand,

Let me reply on the company account as I'm not at home, thank you very much for this information.

This is a much more simplified and clean way to go about it indeed, I did spend some time on Google before composing my code and even asking here.
Thank you for answering even though it is out of the scope of this forum technically, much appreciated.
I have tons of C4D python code that works, so no questions about that so far 🙂

I imagine this will work just fine, going to adapt it into my script.

Hello @Peek,

without any further questions or other postings, we will consider this topic as solved and flag it as such by Friday, 17/06/2022.

Thank you for your understanding,
Ferdinand