2016-12-01 5 views
0

私のスクリプトでは、dest_pathからsource_pathにいくつかのファイルをコピーします。あなたはそれを設定し、それがどのように動作するか見ることができます -私のPythonスクリプトはコピー中にファイルを無視する

しかし、何らかの理由で最初のファイルをコピーし、残りが既にコピーされていると伝えます。私が見ていない、または私が間違っていたことがありますか?私は明らかに間違って何かをした場合、私はちょうどそれを参照してくださいカントとても残念Pythonのためにかなり新しいイム...

import time, shutil, os, datetime 

source_path = r"C:\SOURCE"        # Your Source path 
dest_path = r"C:\DESTINATION"       # Destination path 
file_ending = '.txt'         # Needed File ending 
files = os.listdir(source_path)       # defines 
date = datetime.datetime.now().strftime('%d.%m.%Y')  # get the current date 

while True: 
    print("Beginning checkup") 
    print("=================") 
    if not os.path.exists(source_path or dest_path): # checks if directory exists 
     print("Destination/Source Path does not exist!") 
    else: 
     print("Destination exists, checking files...") 
     for f in files: 
      if f.endswith(file_ending): 
       new_path = os.path.join(dest_path, date,) 
       src_path = os.path.join(source_path, f) 
       if not os.path.exists(new_path): # create the folders if they dont already exists 
        print("copying " + src_path) 
        os.makedirs(new_path) 
        shutil.copy(src_path, new_path) 
       else: 
        print(src_path + " already copied") 
        # shutil.copy(src_path, new_path) 

    print("=================") 
    print('Checkup done, waiting for next round...') 
    time.sleep(10) # wait a few seconds between looking at the directory 
+3

'もしos.path.exists(source_pathまたはdest_path)なら'それはあなたが思うことをしません。 'または'はそのようには動作しません。 – user2357112

+1

また 'source_path'の各ファイルについて' os.path.exists(new_path) 'がチェックされ、初めて' new_path'を作成するので、最初のファイルだけがこのブロックに入ります。 – depperm

+0

new_path = os.path.join(dest_path、date、)実際にはnew_path = os.path.join(dest_path、date、f)でしたが、コピーしたファイルごとに別のフォルダが作成されます。それは私が望んでいたものではないので、私はfを削除しました。どうすればこの@deppemを修正できますか – diatomym

答えて

1

@のuser2357112がif not os.path.exists(source_path or dest_path)を述べたと同じようにあなたが何を考えてやっていません。それはifに通じディレクトリnew_pathを最初に作成するため

if not os.path.exists(source_path) or not os.path.exists(dest_path): 

への変更は、この一つだけのファイルをコピーします。 new_pathディレクトリはその後、存在するディレクトリを作成しません

if f.endswith(file_ending): 
    new_path = os.path.join(dest_path, date,) 
    src_path = os.path.join(source_path, f) 
    if not os.path.exists(new_path): # create the folders if they dont already exists 
     os.makedirs(new_path) 
    if not os.path.exists(os.path.join(new_path,f)): 
     print("copying " + src_path) 
     shutil.copy(src_path, os.path.join(new_path,f)) 
    else: 
     print(src_path + " already copied") 

場合(これは一度だけ起こるはず、これifnew_path初期化だけでなく、ループの外に移動することができる):このような何かが動作するはずです。別のifでそのファイルがそのディレクトリ内に存在するかどうかをチェックし、そうでない場合はファイルをその場所にコピーします。それ以外の場合はメッセージを印刷します。

+0

まずは、ありがとうございます。それは動作します、ありがとう、しかし私はあなたがそれをどのように解決したか完全に理解していませんでした。別のif文を追加しました。あなたはそれを私に説明できるので、コードが何をしているのか分かりますか?私はまだこのスクリプトに関連する仕事がたくさんあるので、そのことを十分に理解したい。理解していただき、ありがとうございました。 – diatomym

+0

@diatomym説明が十分か、もっと必要な場合はお知らせください – depperm

+0

ああ、os.path.join()でファイルをチェックすることもできます。なぜ私はそれが紛らわしいと思ったのですか? – diatomym

関連する問題