设置:Synology 与 Docker 运行 Home-Assistant 与 HACS 集成和 pyscript。
我做了以下两个功能:
@service
def getListOfFiles(dirName):
import os
# create a list of file and sub directories
# names in the given directory
listOfFile = os.listdir(dirName)
allFiles = list()
# Iterate over all the entries
for entry in listOfFile:
# Create full path
fullPath = os.path.join(dirName, entry)
# If entry is a directory then get the list of files in this directory
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
if fullPath.endswith('jpg'):
allFiles.append(fullPath)
elif fullPath.endswith('jpeg'):
allFiles.append(fullPath)
elif fullPath.endswith('png'):
allFiles.append(fullPath)
return allFiles
@service
def slideshow():
import random
import os
import shutil
path = '/Slideshow'
listOfFiles = getListOfFiles(path)
random_image = random.choice([x for x in listOfFiles])
image_path = '{}'.format(random_image)
shutil.copy2(image_path, '/config/www/slide.jpg')
现在一切正常,但目标文件 (slide.jpg) 的大小永远不会正确。它在 10kB - 1000kB 之间变化,而原始图像通常在 7-10 MB 之间。
有什么建议么?
在 Mac 上运行相同的代码(当然,具有不同的源和目标)可以完美运行。
使用 .copyfile 和 .copy 的结果相同