Search code examples
pythonlinuxraspberry-pi

Pass a value from a function to array value in another function


I have a function:

def latest_file_to_backup():
   # returns latest file from a directory
   # called variable $latest_file

I have another function which I'm attempting to make which performs an API call to upload a file:

def auth_to_pcloud(latest_file):
   from pcloud import PyCloud
   # auth details here - properly working
   #pc.uploadfile(files=['/home/pi/<<USE:$latest_file here>>'], path='/a/remote/path/here')

I cannot figure out an easy way to pass the value of $latest_file to the auth_to_pcloud().


Solution

  • You can use an f-string:

    def latest_file_to_backup():
       # returns latest file from a directory
       # called variable $latest_file
    
    def auth_to_pcloud(latest_file):
       from pcloud import PyCloud
       # auth details here - properly working
       pc.uploadfile(files=[f'/home/pi/{latest_file}'], path='/a/remote/path/here')
    
    latest_file = latest_file_to_backup()
    auth_to_pcloud(latest_file)