2016-08-01 44 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)) 
相關問題