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

标签 python linux raspberry-pi

编写脚本保存在闪存驱动器上,用于更新多个树莓派上的代码。我的想法是,我将目录从树莓派 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/

相关文章:

c++ - 如何在 Raspberry Pi 上使用 C++ 将接收到的 UDP 音频数据正确写入 ALSA

python - 我需要通过 .sh 文件安装 python 模块

python - 如何在 <img src> 中添加 django 模板变量?

python - 有效计算均值和中位数

Linux 驱动程序。仅读取 IOCTL 命令有效

linux - 如何在已部署的应用程序中检查 .NET Core 的版本?

Python分割字符串索引超出范围

python - 在 Altair 中保留上一层的工具提示

linux - 使用外部 smtp 服务器发送电子邮件时达到 Amazon EC2 邮件限制

tensorflow - 使用 Bazel 安装 Tensorflow 时调用ProcessError