python - (Python) 如何自动移动创建的文件?

标签 python file automation google-drive-api

我想做的事情:

打印某些内容时,Windows 会创建一个 SPOOL 文件,您可以将其描述为打印文档的精确图像,您可以使用其他软件来读取该图像。 Windows 通常会在打印作业完成后删除 SPOOL 文件,但您可以使其不删除该文件,这样您就可以准确地知道 PC 上的任何打印机打印的内容。

就我而言,我为自己创建了一个 Google 云端硬盘帐户,并在我的电脑上安装了 Google 云端硬盘软件。我还设置 Windows 不删除该文件,以便打印完成后它位于我的电脑上。

正如您可能猜到的那样,我希望 Python 自动检测创建的​​ SPOOL 文件(以 .spl 结尾)并将其复制到 Google Drive。该程序应随 Windows 一起启动,并在 PC 启动时完成其工作。如果可能的话,还应该根据日期创建子文件夹。不知道它是否重要,但是 .spl 文件都在 system32 的文件夹中(可能需要管理员权限)。

我尝试过的:

首先,我是一个 Python 新手,所以你可能需要在这件事上完全灌输给我,对此我感到很抱歉。我在谷歌上搜索过诸如“如何自动移动数据”之类的变体,这导致了很多广泛的结果,但对我来说不是有用的方式。因此,正如我所说,您可能必须向我完全展示如何做到这一点,但我会很感激。

我的设置

我安装了 Windows 7,具有足够的马力和 RAM,足以顺利运行脚本,因为 PC 是工作站。我安装了 python 3.5 32 位,如果需要我可以安装 64 位。

提前致谢

最佳答案

这是我写的一个快速示例:

import time
import shutil
import os
import datetime

source_path = r"C:\system32\whatever"
dest_path = r"C:\asdf\myspecial_file"

while True:
    files = os.listdir(source_path)
    for f in files:
        if f.endswith('.spl'):
            # get the current date
            date = datetime.datetime.now().strftime('%d_%m_%Y')
            new_path = os.path.join(dest_path, date, f)
            src_path = os.path.join(source_path, f)
            # create the folders if they arent already exists
            os.makedirs(new_path)
            shutil.copy(src_path, new_path)
    time.sleep(10) # wait a few seconds between looking at the directory

我用过shutil lib 来复制文件

为了在启动时运行脚本,请查看: http://www.howtogeek.com/138159/how-to-enable-programs-and-custom-scripts-to-run-at-boot/

注意:您可能希望删除 while True 以支持定期使用taskscheduler 运行脚本。这样即使出现错误它也会继续运行。

关于python - (Python) 如何自动移动创建的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40818745/

相关文章:

android - 如何使用 Qt 将文件写入 Android 设备?

testing - TestCafe RequestLogger - 在对每个请求对象进行断言之前如何等待所有响应返回

macos - 删除30天以上的下载内容?

c++ - OLE 自动化是通过 C++ 自动化 IE 的好选择吗?

python vscode modulenotfound错误

python - 使用 Django Oscar 信号

python - crontab 自动 python 脚本不会上传到 dropbox

python - 如何在 python 中通过键盘输入继续 for 循环?

c++ - 将字符串 vector 保存到磁盘不起作用

python - 从Python中的配置文件读取 token