Python 下载FTP文件跳过类似文件
我正在尝试将文件从ftp服务器下载到Python目录。我想检查文件是否存在于我计算机中的某个位置。我想跳过现有文件,只将路径上不存在的文件复制到Python目录 当我运行脚本时,它开始下载文件。但是,它复制了存在和不存在的文档。然后在中途断开。需要进行哪些更正? 我知道有类似的例子,但请让我知道为什么这不起作用。 这是我的剧本Python 下载FTP文件跳过类似文件,python,ftp,download,Python,Ftp,Download,我正在尝试将文件从ftp服务器下载到Python目录。我想检查文件是否存在于我计算机中的某个位置。我想跳过现有文件,只将路径上不存在的文件复制到Python目录 当我运行脚本时,它开始下载文件。但是,它复制了存在和不存在的文档。然后在中途断开。需要进行哪些更正? 我知道有类似的例子,但请让我知道为什么这不起作用。 这是我的剧本 class Testing(): def __init__(self): import ftplib f
class Testing():
def __init__(self):
import ftplib
f = ftplib.FTP('ftp_server_path','login_name','password')
f.cwd('new_directory')
f.cwd('new_directory')
import os
for ftp_file in f.nlst():
for filename in os.listdir("path_where_files_exist"):
if not (ftp_file == filename):
print('Downloading file: %s', ftp_file)
f.retrbinary('RETR '+ ftp_file ,open(ftp_file,'wb').write,rest=0)
break;
f.quit()
这将解决覆盖文件的问题
for ftp_file in f.nlst():
if ftp_file not in os.listdir("path_where_files_exist"):
print('Downloading file: %s', ftp_file)
f.retrbinary('RETR '+ ftp_file ,open(ftp_file,'wb').write,rest=0)
f.quit()
使用2个循环,条件就是问题所在。
您的代码:
for ftp_file in f.nlst():
for filename in os.listdir("path_where_files_exist"):
if not (ftp_file == filename):
#some code
对于一个ftp\u文件
,它将检查每个文件名
。每次它发现ftp\u文件
不等于filename
,它就被下载了
因此,即使文件存在,目录中每隔一个filename
,条件都将返回True,并且ftp\u文件的下载次数将与目录中文件的下载次数相同
希望这有帮助。这应该可以解决覆盖文件的问题
for ftp_file in f.nlst():
if ftp_file not in os.listdir("path_where_files_exist"):
print('Downloading file: %s', ftp_file)
f.retrbinary('RETR '+ ftp_file ,open(ftp_file,'wb').write,rest=0)
f.quit()
使用2个循环,条件就是问题所在。
您的代码:
for ftp_file in f.nlst():
for filename in os.listdir("path_where_files_exist"):
if not (ftp_file == filename):
#some code
对于一个ftp\u文件
,它将检查每个文件名
。每次它发现ftp\u文件
不等于filename
,它就被下载了
因此,即使文件存在,目录中每隔一个filename
,条件都将返回True,并且ftp\u文件的下载次数将与目录中文件的下载次数相同
希望这有帮助。是的。我错过了!谢谢,是的。我错过了!谢谢