A
A
Andrey Gladchenko2021-05-13 00:38:21
Python
Andrey Gladchenko, 2021-05-13 00:38:21

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).

spoiler
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()

When downloading a file, line breaks occur, all the time in different places.
unknown.png
An example of logs from the server and what happens (line breaks are marked in red).

How can this be fixed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2021-05-13
@dimonchik2013

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 question

Ask a Question

731 491 924 answers to any question