gpt4 book ai didi

python - 在 RPi 3 上复制文件的奇怪脚本行为

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:10 26 4
gpt4 key购买 nike

编写脚本保存在闪存驱动器上,用于更新多个树莓派上的代码。我的想法是,我将目录从树莓派 sd 卡备份到闪存驱动器,然后将新文件从闪存驱动器复制到 pi 中的 sd 卡。

我得到的行为是所有内容都从闪存移动到 SD 卡/home/pi/目录,没有任何内容移动到闪存驱动器。

我试过将 shutil.move 与 copy 和 copy2 参数一起使用,我试过只使用 copy 或 copy2。

#backupDir = /media/pi/mountpoint/SimulatorBackup which is where it should be
backupDir = os.path.join(os.path.dirname(os.path.realpath(__file__)),'SimulatorBackup')
#flashDir = /media/pi/mountpoint/Simulator which is correct
flashDir = os.path.join(os.path.dirname(os.path.realpath(__file__)),'Simulator')
#homeDir = /home/pi/Simulator which is correct
homeDir = os.path.join(os.path.expanduser('~'),'Simulator')

def copyAllFilesinDir(srcDir, dstDir):
# Check if both the are directories
if os.path.isdir(srcDir) and os.path.isdir(dstDir) :
# Iterate over all the files in source directory
print("Copying from ",srcDir, "to ", dstDir)
#this returns Copying from /home/pi/Simulator to /media/pi/mountpoint/SimulatorBackup on first run
#Copying from /media/pi/mountpoint/Simulator to /home/pi/Simulator on second run
for filePath in glob.glob(srcDir + '*'):
print(filePath)
#this returns /home/pi/Simulator
#for file in srcDir # Move each file to destination Directory
shutil.move(filePath, dstDir, shutil.copy);
else:
print("srcDir & dstDir should be Directories")

#check for the backup directory before deleting it and remaking
if os.path.isdir(backupDir):
#empty current backup directory and remake
shutil.rmtree(backupDir)
os.makedirs(backupDir)
else:
os.makedirs(backupDir)

#copy files to backup directory on flash drive
copyAllFilesinDir(homeDir,backupDir)

#move new files to Simulator directory on raspberry pi
if not os.path.isdir(homeDir):
os.makedirs(homeDir)
copyAllFilesinDir(flashDir,homeDir)

没有错误。我最终得到/home/pi/Simulator/Simulator 和/home/pi/Simulator/SimulatorBackup 都已填充,并且闪存驱动器上没有目录。我希望闪存驱动器在其根目录中包含/Simulator/和/SimulatorBackup,并且/home/pi/Simulator 包含闪存驱动器/Simulator/目录中的新文件。

最佳答案

I backup the directory from the raspberry pi sd card, to the flash drive, then copy the new files to from the flash drive to the sd card in the pi.

您似乎将 copymove 混淆了 - shutil.move(filePath, dstDir, shutil.copy) moves 文件从闪存驱动器到 sd 卡(即它们从闪存驱动器中删除),即因为 for filePath in glob.glob(srcDir + '*' ),所有名称​​以开头的/media/pi/mountpoint/Simulator文件,当然包括/media/pi/mountpoint/SimulatorBackup 。也许您的意思是 for filePath in glob.glob(srcDir + '/*')

The behavior I get is that everything is moved from the flash, to the SD cards /home/pi/ directory, nothing is moved to the Flash drive.

什么都没有移到闪存驱动器是不正确的 - 只是 …/Simulator* 在第二步中移回了。

关于python - 在 RPi 3 上复制文件的奇怪脚本行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57677304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com