基本上我想在我的腳本中執行的操作是將一些文件從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
如果不是'os.path.exists(new_path)',將檢查'source_path'中的每個文件,並且只有第一個文件會進入這個塊,因爲你在第一次通過 – depperm
創建'new_path' new_path = os.path.join(dest_path,date,)實際上是new_path = os.path.join(dest_path,date,f),但是它會爲它複製的每個文件創建另一個文件夾。那不是我想要的,所以我刪除了f,現在它做到了。我該如何解決這個問題@deppem – diatomym