基本上我想在我的脚本中执行的操作是将一些文件从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