Python Forum
Filter Excel and Convert an Excel File - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Filter Excel and Convert an Excel File (/thread-33649.html)



Filter Excel and Convert an Excel File - giddyhead - May-13-2021

Hello everyone,

The following is my attempt to filter the last modified xls file on a ftp server and convert that file to a csv on the same server.

from datetime import datetime

import ftplib

import ftputil

import pandas as pd

# Download some files from the login directory.

with ftputil.FTPHost('ftp.123.com', 'username', 'password') as host:

flz= []

name2 = host.curdir

names = host.listdir(host.curdir)

print(host.curdir)

print(name2)

for name in names:

if host.path.splitext(name)[1] == '.xls':

mtime = host.path.getmtime(name)

converttime = datetime.fromtimestamp(mtime).strftime('%Y-%m-%d %H:%M:%S')

flz.append(name)

print(name,converttime)

for i in range(0, len(flz)):

if i == (len(flz) - 1):

print("This is the last element : " + str(flz[i]))

# Read the file

if host.path.isfile(str(flz[i])):

# Read the file

read_file = pd.read_excel(host.getcwd()(f'{flz[i]}'))

# Reconstruct file name, converting extension from '.xls' to '.csv'

output_csv = f"{host.getcwd()(str(flz[i]).name.replace('.xls', ''))}.csv"

# Create output csv file

read_file.to_csv(output_csv, index=None, header=True)

# Read csv file into DataFrame

df = pd.DataFrame(pd.read_csv(f"{host.curdir(get.cwd() / 'output_csv')}"))

# Output DataFrame

print(df)

print('Done')
When ran I get an error message of:
Traceback (most recent call last):
  File "C:\Users\mrdrj\Desktop\Desk--2021 April 23\Desktop(Update)\ftpdownload.py", line 29, in <module>
    read_file = pd.read_excel(host.getcwd()(f'{flz[i]}'))
TypeError: 'str' object is not callable
How can I solve the filter and convert to csv ftp issue? Thanks