【问题标题】:Create new folder with timestamp and then move files to new folder创建带有时间戳的新文件夹,然后将文件移动到新文件夹
【发布时间】:2016-08-01 19:27:46
【问题描述】:

我遇到了这个问题...我想创建一个带有时间戳名称的新文件夹。然后我想把一堆文件移进去。

我想不通!

import shutil, os, time   
timestr = time.strftime("%Y%m%d")
Sourcepath = r'Z:\\test'
if not os.path.exists(Sourcepath):
        os.makedirs(Sourcepath+timestr)
source = os.listdir(Sourcepath)    
destinationpath = (Sourcepath+timestr)     
for files in source:
    if files.endswith('.json'):      
shutil.move(os.path.join(source,files),os.path.join(destinationpath,files))

【问题讨论】:

  • 请在代码的每一行缩进四个空格(如果您的代码缩进更多,则更多)。然后阅读起来会更清晰。
  • 运行代码时究竟发生了什么?你有追溯吗?还是不是您期望的结果?

标签: python timestamp shutil


【解决方案1】:

这能解决您的问题吗?注意最后一行的缩进

import shutil, os, time   
timestr = time.strftime("%Y%m%d")
Sourcepath = r'Z:\\test'
if not os.path.exists(Sourcepath):
        os.makedirs(Sourcepath+timestr)
source = os.listdir(Sourcepath)    
destinationpath = (Sourcepath+timestr)     
for files in source:
    if files.endswith('.json'):
        shutil.move(os.path.join(source,files),os.path.join(destinationpath,files))

【讨论】:

    猜你喜欢
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多