python - 按创建/修改日期查找文件,然后 move 到 Python 中的另一个目录

标签 python python-2.7 move last-modified shutil

第一个问题。我是编程新手,更不用说 python 了。正如标题所说,我试图找到在过去 24 小时内创建或修改的文件,然后将这些文件 move 到另一个目录。我可以找到这些文件,但我不知道如何 move 满足此条件的文件。到目前为止我的脚本:


for root,dirs,files in os.walk('source\folder'):
for file_name in files: now = dt.datetime.now() before = now - dt.timedelta(hours=24) path = os.path.join(root,file_name) st = os.stat(path)
mod_time = dt.datetime.fromtimestamp(st.st_ctime) if mod_time < before: print('%s modified %s'%(path,mod_time))

我尝试使用 shutil move 输出但出现错误;

TypeError: coercing to Unicode: need string or buffer, datetime.datetime found

我试图在网上找到解决方案,但一直没有成功。甚至不确定我是否可以用我构建的方式做我想做的事情?提前致谢。

最佳答案

代替:

shutil.move(mod_time, 'dest\path')

做:

shutil.move(os.path.join(root, file_name), 'dest\path')

这会将文件名而不是日期传递给该函数。

关于python - 按创建/修改日期查找文件,然后 move 到 Python 中的另一个目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31689355/

相关文章:

python - 在 python 2.7 中使用 PI

python-2.7 - Scikit-learn KNN 中的归一化

c++ - 是否有 STL 算法或 C++ 习惯用法根据谓词将项目从一个 std::list move 到另一个?

python:for循环错误中的Input()

python - 在 uwsgi chain-raload 期间预热 django 应用程序

python - pycharm "Note that you cannot install any Python packages into Docker-based project interpreters."

python - 如何在不同的数据源之间切换?

c++ - 如何延长局部变量的生命周期或使用引用的正确方法是什么

c++ - 为什么可以通过构造函数将临时值分配给引用?

python - 在 Numpy 中交换行会产生一个嵌入式数组