私のスクリプトでは、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
'もしos.path.exists(source_pathまたはdest_path)なら'それはあなたが思うことをしません。 'または'はそのようには動作しません。 – user2357112
また 'source_path'の各ファイルについて' os.path.exists(new_path) 'がチェックされ、初めて' new_path'を作成するので、最初のファイルだけがこのブロックに入ります。 – depperm
new_path = os.path.join(dest_path、date、)実際にはnew_path = os.path.join(dest_path、date、f)でしたが、コピーしたファイルごとに別のフォルダが作成されます。それは私が望んでいたものではないので、私はfを削除しました。どうすればこの@deppemを修正できますか – diatomym