[英]Delete some csv files from FTP Server using ftplib
I am trying to delete some csv files from a ftp server.我正在尝试从 ftp 服务器中删除一些 csv 文件。 Uses
ftplib
library.使用
ftplib
库。 The files are not getting deleted from the server.这些文件没有从服务器中删除。 I have read-write access to the server.
我对服务器具有读写访问权限。 No error messages even showing.
甚至没有显示错误消息。 I have around 20 files to delete, I can printout the filenames successfully as well.
我有大约 20 个文件要删除,我也可以成功打印出文件名。
dir_files = [] # to store all files in the root
try:
ftp = ftplib.FTP(os.environ.get("TIENKANG_HOST")) # port is 21 by default
ftp.login(
user=os.environ.get("TIENKANG_USER"), passwd=os.environ.get("TIENKANG_PASS")
)
ftp.encoding = "unicode_escape"
ftp.dir(dir_files.append)
for file_info in dir_files:
if "DATA_collection" in file_info:
if filename not in file_info:
old_filename = file_info.split(" ")[-1]
if old_filename[-3:] == "csv":
# data can be exported to db if necessary before removing
# print(old_filename)
ftp.delete(old_filename)
except Exception as e:
print(e)
finally:
ftp.quit()
My files are in root of the server.我的文件位于服务器的根目录中。 below given how I sort out filenames.
下面给出了我如何整理文件名。 (unfortunately ftp.nlst leads to error)
(不幸的是 ftp.nlst 导致错误)
I had to use ftp.encoding = "unicode_escape"
, to get all filenames in the ftp server.我必须使用
ftp.encoding = "unicode_escape"
来获取 ftp 服务器中的所有文件名。 Without changing the encoding I gets error since there is machine related files exist in the ftp server that is not in standard unicode encoding.在不更改编码的情况下,我会出错,因为 ftp 服务器中存在与标准 unicode 编码无关的机器相关文件。
So, before deleting I changed encoding back to ftp.encoding = "utf-8"
and all went prefectly.因此,在删除之前,我将编码改回
ftp.encoding = "utf-8"
并且一切顺利。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.