Answer the question
In order to leave comments, you need to log in
How to fix line breaks when downloading a file (ftplib)?
There is an FTP connection code.
I am connected, I find out the list of files.
I'm downloading files.
The files on the FTP server are updated periodically.
When updating, I want to download the file and overwrite the current one, update it (synchronize).
ftp = ftplib.FTP()
ftp.connect(host, port)
loginRepsonse = ftp.login(user, password) #подключаюсь
ftp.encoding = 'utf-8'
ftp.cwd('/Logs')
log_list = []
log_name = ftp.retrlines('LIST', log_list.append) #смотрю список файлов
for files in log_list:
filename = re.search('\d\d\:\d\d\s(.*).log$', files) #выбираю файлы, названия файлов
if filename:
filename1 = filename.group(1)+".log"#задаю соответствие файла .log
content = []
ftp.retrbinary('RETR ' + filename1, content.append, 8 * 1024)
for line in content:
line = line.decode("utf-16le").strip().splitlines()
Answer the question
In order to leave comments, you need to log in
write the correct code
1) do not download the appending file, but only gz after log rotation, for example,
or
2) insert the .strip().splitlines() check here, you kill the line feed with this
another option - apply wget to the download, although with appending everything it will still be crap
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question