2014-02-19 4 views
1
def ignore_list(path, files): 
    filesToIgnore = [] 
    for fileName in files: 
     fullFileName = os.path.join(os.path.normpath(path), fileName) 
     if not os.path.isdir(fullFileName) and not fileName.endswith('pyc') and not fileName.endswith('ui') and not fileName.endswith('txt') and not fileName == '__main__.py' and not fileName == 'myfile.bat': 
      filesToIgnore.append(fileName) 

    return filesToIgnore 

# Start of script  
shutil.copytree(srcDir, dstDir, ignore=ignore_list) 

복사 할 파일이 포함 된 if 행을 변경하고 싶습니다. 여기서 파일 이름을 별도로 지정해야하지만 모든 파일 이름이 들어있는 목록에서 파일 이름을 가져야하는 것처럼 변경하려고합니다.파일을 반복적으로 복사

어떻게하면됩니까?

+1

파일 이름 목록은 어떻게 생겼습니까? 만약 무시할 파일 목록이라면'ignore ='에 그냥 넘기지 않으시겠습니까? – Bach

+0

이 방법을 올바르게 사용하면 ignore_list가 경로 디렉토리를 통해 실행하고 if와 일치하는 파일을 찾아 복사하지 않아야하는 파일 목록을 반환해야합니다. – flazzarini

+0

복사 할 파일이 "if"줄에 있습니다.이 줄이 필요하기 때문에 "if"조건 (* .pyc, * .txt, * .ui, __main__ 포함)이있는 "if"조건을 확인해야합니다. .py, myfile.txt) – Patrick

답변

1

귀하의 질문은에서 내가 이해할 수있는 무엇, 당신은 간단하게 작성할 수 fileNameList은 복사 할 파일 이름의 목록입니다

if fileName not in fileNameList: 
    filesToIgnore.append(fileName) 

.

업데이트 : 당신은 또한 다음과 같은 방법을 쓸 수

fName, fileExtension = os.path.splitext(fileName) 
if fileName not in fileNameList or fileExtension not in allowedExtensionList: 
    filesToIgnore.append(fileName) 
+0

* .pyc, * .ui, * .txt 및 __main__.py, myfile.txt가 포함되어 있기 때문에 코드에 "if"행을 대체 할 수 있습니까? – Patrick

+0

If 당신은 당신의 명부에있는 파일의 가득 차있는 이름을 (연장에) 제공하고있다, 당신은 파일 확장을위한 수표를 둘 필요가없는 다. 그냥 허용/허용 파일을 넣어. –

+0

너무 많은 .pyc, .ui 및 .txt 파일이 있으므로 모든 파일의 전체 이름을 지정할 수 없습니다. – Patrick

0

.

def ignore_files(path, extensions, wantedfiles): 
     ignores = [] 

     for root, dirs, filenames in os.walk(path): 
      for filename in filenames: 
      if any(filename.endswith(_) for _ in extensions): 
        ignores.append(os.path.join(os.path.normpath(path), filename)) 
      elif filename in wantedfiles: 
        ignores.append(os.path.join(os.path.normpath(path), filename)) 

     return ignores 

# Start of script 
ignore_list = ignore_files(srcDir, ['pyc', 'uid', 'txt']) 
shutil.copytree(srcDir, dstDir, ignore=ignore_list) 
+0

응답 코드에 특정 파일을 복사하는 조건을 포함하는 방법 – Patrick

+0

코드를 업데이트했습니다. 예를 들어 "main.py"와 같은 특정 파일도 포함해야합니다. 별도의 목록에 전달해야합니다. – flazzarini

+0

shutil.copytree (srcDir, dstDir, ignore = ignore_list) -이 줄에서 오류가 발생했습니다. TypeError : 'list'개체를 호출 할 수 없습니다. – Patrick

관련 문제