2011-04-04 98 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

相關問題