2011-04-04 96 views
5

我想移动很多文件。这些文件的路径存储在一个列表中。我想保持整个目录结构,但想将它们移动到不同的文件夹。shutil移动文件保持相同的目录结构

因此,例如文件 d:\测试\ test1的\ test1.txt的 d:\测试\ test1的\的test2.txt

我想将它们移动到C:从d \:\和保持目录结构。我应该怎么做呢?

这是代码我有,它不工作

import os, fnmatch 
import shutil 


f=open('test_logs.txt','r') #logs where filenames are stored with filenames as first entry 

for line in f: 
    filename=line.split() 
    output_file="C:" + filename[0].lstrip("D:") 
    shutil.move(filename[0],output_file) 

我读的文件名很好,我可以生成目标文件名罚款,但是当我运行它,它给了我一个错误说“不这样的文件或目录“(并给出输出文件名的路径)。

回答

5

我想你想是这样的:

import sys 
import os 
import shutil 

# terminology: 
# path = full path to a file, i.e. directory + file name 
# directory = directory, possibly starting with a drive 
# file name = the last component of the path 

sourcedrive = 'D:' 
destdrive = 'C:' 

log_list_file = open('test_logs.txt', 'r') 
for line in log_list_file: 
    sourcepath = line.split()[0] # XXX is this correct? 
    if sourcepath.startswith(sourcedrive): 
     destpath = sourcepath.replace(sourcedrive, destdrive, 1) 
    else: 
     print >>sys.stderr, 'Skipping %s: Not on %s' % (sourcepath, sourcedrive) 
     continue 

    destdir = os.path.dirname(destpath)  

    if not os.path.isdir(destdir): 
     try: 
      os.makedirs(destdir) 
     except (OSError, IOError, Error), e: 
      print >>sys.stderr, 'Error making %s: %s' % (destdir, e) 
      continue 

    try: 
     shutil.move(sourcepath, destpath) 
    except (OSError, IOError, Error), e: 
     print >>sys.stderr, 'Error moving %s to %s: %s' % (sourcepath, destpath, e) 

你也想删除源目录,如果它是空的?

+0

谢谢米克尔,谢谢sendrele – Illusionist 2011-04-04 03:02:58

2

更新:啊,好的,我看到问题 - shutil.move不会复制到不存在的目录。要做你想做的事情,你必须首先创建新的目录树。因为它是一个比较安全使用内置的移动功能,而不是推出自己的拷贝和删除过程中,你可以这样做:

with open('test_logs.txt','r') as f: 
    files_to_copy = [line.split()[0] for line in f] 
paths_to_copy = set(os.path.split(filename)[0] for filename in files_to_copy) 

def ignore_files(path, names, ptc=paths_to_copy): 
    return [name for name in names if os.path.join(path, name) not in ptc] 

shutil.copytree(src, dst, ignore=ignore_files) 

for filename in files_to_copy: 
    output_file="C:" + filename.lstrip("D:") 
    shutil.move(filename, output_file) 

让我知道,如果不工作


原始帖子:如果你只想移动一些文件,最好的选择就是使用shutil.copytreeignore关键字。假设你的文件列表包括完整路径和目录(即['D:\test\test1\test1.txt', 'D:\test\test1\test2.txt', 'D:\test\test1']),创建一个ignore_files功能,并使用它像这样:

files_to_copy = ['D:\test\test1\test1.txt', 'D:\test\test1\test2.txt', 'D:\test\test1'] 

def ignore_files(path, names, ftc=files_to_copy): 
    return [name for name in names if os.path.join(path, name) not in ftc] 

shutil.copytree(src, dst, ignore=ignore_files) 

然后,你可以删除files_to_copy文件:

for f in files_to_copy: 
    try: 
     os.remove(f) 
    except OSError: # can't remove() a directory, so pass 
     pass 

我测试这个 - 确保你包含你想要复制的路径以及files_to_copy中的文件 - 否则,这将删除文件而不复制它们。

+0

是的,我没有移动所有的文件,只有其中一些。 – Illusionist 2011-04-04 00:47:28

+0

问题是保持相同的目录结构。 – Illusionist 2011-04-04 00:47:53

+0

未移动的文件会发生什么情况?他们只是待在原地吗? – senderle 2011-04-04 00:56:09

相关问题