【问题标题】:How to Move the files from one directory to another directory?如何将文件从一个目录移动到另一个目录?
【发布时间】:2019-09-26 20:47:53
【问题描述】:

目录文件名应附加当前日期(yyyy-mm-dd)

import shutil
import os

source = '/path/to/source_folder'
dest1 = '/path/to/dest_folder'

files = os.listdir(source)

for f in files:
        shutil.move(source+f, dest1)

【问题讨论】:

  • 你现在面临的问题是什么

标签: python python-3.x file unix file-copying


【解决方案1】:

这可能会对你有所帮助。

 import os
 import datetime
 import shutil
 now = datetime.datetime.now()

 path  ='f:\\python\\testdir\\'
 dest_folder='f:/python/testdir/copyfolder/'

 files=[]
 #check if destination folder exists if not create one
 if os.path.exists(dest_folder):
    #ignore nothing to do
    pass
 else:
   print("not exits")
   os.mkdir(dest_folder)
 for r, d, f in os.walk(path):
   for file in f:
      # if '.txt' in file:
         #print(os.path.join(r, file))
         files.append(os.path.join(r, file))
 for f in files:
    print(f)
    #extract the file name from the path
    base_name=os.path.basename(f)

    #extracting the file name and it's extension 
    file_name,ext=os.path.splitext(base_name)

    #print(ext)
    #file_name+='_'+now.strftime("%Y-%m-%d %H:%M")

    #append the current time stamp and it's extension and move the file to new destination
     shutil.copy2(f,'f:/python/testdir/copyfolder/'+file_name+'-'+now.strftime("%Y-%m-%d")+ext)

    #print(file_name)

【讨论】:

    猜你喜欢
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 2014-12-04
    • 2021-11-12
    • 2016-06-09
    相关资源
    最近更新 更多