Search code examples
pythonunixicloud

how to create a file in Mac cloud drive using python


I am trying to create a file in my cloud drive in a python code. When i create a file at this location"/Users/agauravdeep/Library/Mobile Documents/com~apple~CloudDocs/Untitled.rtf " using any app, it works but through code, i am unsuccessful so far(no errors though) even after trying through super user.

Code is pretty straightforward

# -*- coding: utf-8 -*-
#!/usr/bin/python

from os.path import expanduser
def createBugsFileInitiallyIfNotPresent():
                    global bugsFileLocation
                    bugsFileLocation =  expanduser("~")+"/Library/Mobile Documents/com~apple~CloudDocs/abc.txt"
                    if(os.path.isfile(bugsFileLocation)):
                            f = open(bugsFileLocation, 'w+')
                            f.write(" dddd ")

This works Also, vi ~/Library/Mobile\ Documents/com~apple~CloudDocs/abcd.txt but when i put quotes around path it doesn't work.

Any suggestions would be very helpful.


Solution

  • You are checking if the file exists, but you say that you want to create it. If you plan to create a new file, remove the if statement:

    from os.path import expanduser
    def createBugsFileInitiallyIfNotPresent():
                    global bugsFileLocation
                    bugsFileLocation =  expanduser("~")+"/Library/Mobile Documents/com~apple~CloudDocs/abc.txt"
                    f = open(bugsFileLocation, 'w+')
                    f.write(" dddd ")