修复编程单下发ftp路径

This commit is contained in:
jinling.yang
2024-06-25 17:54:08 +08:00
parent 27ba002b51
commit 88ef9c106d
8 changed files with 61 additions and 15 deletions

View File

@@ -21,6 +21,32 @@ class FtpController():
except Exception:
logging.info("ftp连接失败")
def file_exists_1(self, path):
# 检查文件是否存在于FTP服务器上
try:
logging.info("path:%s" % path)
logging.info("dirname:%s" % os.path.dirname(path))
directories = os.path.normpath(path).split(os.path.sep)
# 切换到上级目录
logging.info("当前目录: %s" % self.ftp.pwd())
current_dir = '/'
for directory in directories:
if directory:
# 检查目录是否存在
if directory in ['ZM', 'FM', 'YC', 'HC', 'QC', 'ZC']:
self.ftp.cwd(directory)
if directory in ['NC']:
self.ftp.cwd(directory)
if directory not in ['home', 'ftp', 'ftp_root', 'NC']:
# 切换到新的目录
current_dir = os.path.join(current_dir, directory)
logging.info("current_dir:%s" % current_dir)
self.ftp.cwd(directory)
return os.path.basename(path)
except Exception as e:
logging.error(f"Error checking file: {e}")
return False
def file_exists(self, path):
# 检查文件是否存在于FTP服务器上
try:
@@ -32,8 +58,23 @@ class FtpController():
logging.error(f"Error checking file: {e}")
return False
# 下载目录下的pdf文件(程序单)
def download_program_file(self, target_dir, serverdir):
if not os.path.exists(serverdir):
os.makedirs(serverdir)
try:
logging.info('FTP目录:%s' % target_dir)
logging.info("进入FTP目录 ")
# self.ftp.cwd(target_dir) # 切换工作路径
# logging.info('FTP目录:%s' % target_dir)
remotenames = self.ftp.nlst()
logging.info('FTP目录文件:%s' % remotenames)
for file in remotenames:
server = os.path.join(serverdir, file)
if file.find(".pdf") != -1:
self.download_file(server, file)
except:
return False
# # 检测字符串的编码
# def detect_encoding(self, s):