-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp_handle.py
59 lines (52 loc) · 2.45 KB
/
ftp_handle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
import xml.etree.ElementTree as ET
import pandas as pd
from info import ftp_params
from Connectors import FtpsConnector as FTPs_C
class Ftp_handler:
ftp_cnnctn = None
def csv_handler_first_call(self):
if os.path.exists("total_payments.csv"):
os.remove("total_payments.csv")
self.ftp_cnnctn = FTPs_C(ftp_params) # open ftp connection
dirs = self.ftp_cnnctn.ftps.nlst()
self.ftp_cnnctn.ftps.cwd(dirs[0])
folders = self.ftp_cnnctn.ftps.nlst()
local_filename = 'current_payments.tsv'
total_filename = 'total_payments.csv'
with open(total_filename, 'a') as fin:
for i in folders:
with open(local_filename, 'wb') as fin_local:
self.ftp_cnnctn.ftps.retrbinary("RETR " + i, fin_local.write)
with open(local_filename, 'r') as fin_local:
fin.write(fin_local.read())
self.ftp_cnnctn.ftps.quit() # close ftp connection
def xml_handler_first_call(self):
if os.path.exists("total_waybill.csv"):
os.remove("total_waybill.csv")
self.ftp_cnnctn = FTPs_C(ftp_params) # open ftp connection
dirs = self.ftp_cnnctn.ftps.nlst()
self.ftp_cnnctn.ftps.cwd(dirs[1])
folders = self.ftp_cnnctn.ftps.nlst()
local_filename = 'current_waybill.xml'
total_filename = 'total_waybill.csv'
with open(total_filename, 'a') as fin:
for i in folders:
with open(local_filename, 'wb') as fin_local:
self.ftp_cnnctn.ftps.retrbinary("RETR " + i, fin_local.write)
with open(local_filename, 'r') as fin_local:
tree = ET.parse(fin_local)
root = tree.getroot()
for child in root:
r=[child.attrib["number"], child.attrib["issuedt"], root[0][0].text, root[0][1].text]
for element in child:
for element in element:
new=element.text
r.append(new)
str = ''
for i in r:
str = str + i + ';'
str = str + "\n"
fin.write(str)
print (r)
self.ftp_cnnctn.ftps.quit() # close ftp connection