【问题标题】:Run bash script to automatically copy contents from USB device after USB device mount运行 bash 脚本以在 USB 设备挂载后自动从 USB 设备复制内容
【发布时间】:2019-07-22 06:49:04
【问题描述】:

我有一个 python 脚本可以将所有文件从 USB 存储设备复制到我的 Ubuntu 机器中的目标文件夹。我以前从未使用过 Python 编程。

import os
import shutil
from shutil import copytree, ignore_patterns

files = os.listdir('/media/user/HP drive')

destination = '/home/user/Documents/Test/%s'
try :
    for f in files:
        source = '/media/user/HP drive/%s' % f
        copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))    
except Exception as e:
    print(e)

上面的脚本运行良好,但它在 Test 文件夹内创建了一个文件夹 %s 并带有锁定符号。当我删除 %s 并使用

destination = '/home/user/Documents/Test/'

它给了我[Errorno 17] file exists

这是我想在挂载 USB 设备时运行的 bash 脚本(copy.sh)。

#!/bin/sh

python /var/www/html/copy_flash.py    #This doesn't work.

# echo "My message" > /var/www/html/thisisaverylongfilename.txt #This works

所以 python 命令不起作用,但是当我插入 USB 时 echo 命令起作用。

这是我在 /etc/udev/rules.d/test.rules

中添加的行
ACTION=="add",KERNEL=="sdb*", RUN+="/var/www/html/copy.sh"

是不是因为bash脚本运行时U盘没有准备好?

  1. 如何将 USB 驱动器的内容复制到常规文件夹而不是 %s 中?
  2. 如何实际复制内容?

【问题讨论】:

    标签: python bash ubuntu


    【解决方案1】:

    给出python的完整路径如:

    /usr/bin/python /var/www/html/copy_flash.py
    

    【讨论】:

    • 不行不行。当我从终端运行 bash 脚本时,正在运行相同的命令。 /var/www/html/copy.sh
    【解决方案2】:

    为了不使用 %s 你可以使用 format 方法。

    source = '/media/users/HP/{path}'.format(path=your_filename_here)
    

    您可以在大括号中使用任何名称来创建格式的关键字参数。您还可以使用转换为位置参数的数字。 一个例子:

    'Hello {0}! Good {1}'.format('DragonBorn', 'Evening!')
    

    shutil 的copytree 也要求目标不存在。因此,您需要检查目标是否存在,如果存在则将其删除。您可以为此使用 os.rmdir 和 os.path.exists。 shutil 也可能有一个等价的功能。

    https://docs.python.org/3.5/library/shutil.html#shutil.copytree

    您可以执行此检查并使用以下命令复制树:

    if os.path.exists(destination):
        if os.listdir(): # If the directory is not empty, do not remove.
            continue
        os.rmdir(destination)
    shutil.copytree(source, destination)
    

    如果要删除目录下的整个树,可以使用 shutil.rmtree()。

    if os.path.exists(destination):
        shutil.rmtree(destination)
    

    【讨论】:

    • 谢谢。是的,它现在可以按我的意愿工作,除了我找到答案的第二个问题,我将添加它
    【解决方案3】:

    解决方案 1:如何将 U 盘内容复制到常规文件夹而不是 %s 中?

    我从Ethan's answer 开始工作。

    解决方案 2:如何实际复制内容?

    好的,所以我从this answer 发现了 systemd,它比 udev 规则的优势是脚本在挂载后真正触发,而不是在添加系统设备后触发这就是为什么我的 python 脚本无法复制文件的原因,因为该脚本在设备实际安装之前就已经运行了。

    我删除了文件/etc/udev/rules.d/test.rules

    并在 /etc/systemd/system/copy.service 中创建了一个新文件,其内容为:

    [Unit]
    Description=My flashdrive script trigger
    Requires=media-YourMediaLabel.mount
    After=media-YourMediaLabel.mount
    
    [Service]
    ExecStart=/home/you/bin/triggerScript.sh
    
    [Install]
    WantedBy=media-YourMediaLabel.mount
    

    运行此命令sudo systemctl list-units -t mount。找到您的设备并将上面的media-YourMediaLabel.mount 替换为您的设备单元。

    然后你必须启动/启用服务:

    sudo systemctl start copy.service
    sudo systemctl enable copy.service
    

    就是这样。挂载后,您的 USB 设备的内容将自动复制到您的目标位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 2017-06-01
      • 1970-01-01
      相关资源
      最近更新 更多