python 将一个函数中的值传递给另一个函数中的数组值

swvgeqrz  于 2023-10-14  发布在  Python
关注(0)|答案(3)|浏览(119)

我有一个功能:

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

我有另一个函数,我试图使它执行一个API调用上传文件:

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')

我无法找到一种简单的方法将$latest_file的值传递给auth_to_pcloud()

bvk5enib

bvk5enib1#

你为什么不试试简单的方法?将第一个方法中的值放入变量中,并将其传递给upload方法。

latest_file = latest_file_to_backup()
auth_to_pcloud(latest_file)
o2rvlv0m

o2rvlv0m2#

你可以使用f字符串:

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)
lokaqttq

lokaqttq3#

也许这能解决你的问题
1.返回你的文件作为二进制
1.将二进制数据写入临时文件
1.将临时文件传递给第二个函数

def latest_file_to_backup():
    #codes 
    return b'file'  # Replace your file in binary format
    

def auth_to_pcloud(file):
    # Your code 

# generate binary data to pass as argument
binary_data = latest_file_to_backup()

# Create a temporary file and write the binary data
with open("temp_file.bin", "wb") as temp_file:
    temp_file.write(binary_data)

# Pass the file to the second function
auth_to_pcloud("temp_file.bin")

相关问题