Copy files in folder up one directory in python

大憨熊 提交于 2019-12-04 07:45:56

The shutil module can do this, specifically the copyfile, copy, copy2 and copytree functions. http://docs.python.org/library/shutil.html

You probably want something along these lines:

import os
import shutil

fileList = os.listdir('path/to/source_dir')
fileList = ['path/to/source_dir/'+filename for filename in fileList]

for f in fileList:
    shutil.copy2(f, 'path/to/dest_dir/')

You can of course filter some file names out during the call to os.listdir(). For example,

fileList = [filename for filename in os.listdir('path/to/source_dir') if filename[-3] is '.txt']

instead of fileList = os.listdir('path/to/source_dir') to get just the .txt files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!