2016-08-01 75 views
0

我遇到了这个问题...我想创建一个带有时间戳名称的新文件夹。然后,我想将一堆文件移入其中。使用时间戳创建新文件夹,然后将文件移动到新文件夹

我找不出来!

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)) 
+0

请用四个空格缩进代码的每一行(如果您的代码缩进更多,请更多)。那么阅读会更清晰。 – Ben

+1

当你运行你的代码时究竟发生了什么?你有追溯吗?或者只是不是你期望的结果? –

回答

0

这是否解决了您的问题。最后一行的通知缩进

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)) 
相关问题