Find and download the file name of the CSV file that you want to download in python ftplib!

Asked 1 years ago, Updated 1 years ago, 71 views

Prerequisites/What you want to achieve

I would like to download the CSV file in python ftplib.At that time, the date part of the file name changes every day, so I will search by regular expression and download it.
However, I received the following error, so please tell me how to resolve it.

Problems/Error Messages you are experiencing

TypeError: findall() missing 1 required positional argument: 'string'

Source Codes Affected

import ftplib
from logging import getLogger, StreamHandler, Formatter, DEBUG
import re

## FTP Information
HOST='***.ne.jp'
PORT = 21
USER='***'
PASSWORD='***'
DIRECTORY='/***/***'
## Generating Filename to Retrieve

FILE_NAME=(re.findall('item2019????-?.csv'))#←I want to search here by regular expression!
print(FILE_NAME)

Tried

I said FILE_NAME=(re.findall('item2019????-?.csv') in re, but I didn't know what to do to make it str type.

Supplementary information (for example, FW/Tool Version)

Python 3.7.3
Windows 7 jupyter notebook

python3 windows ftp

2022-09-30 21:45

1 Answers

re.findall requires at least two arguments: the search pattern and the search string.
The code for the question is an error because only the search pattern is specified in the argument.

The sample code below is an excerpt of a simple file listing using ftp.nlst to find the appropriate file name in a regular expression.
We use re.match, but the arguments are similar to re.findall just because the matching method and return value are different.

import re

# items=ftp.nlst(".")# File list returned in array
items=['item201912301913-1.csv', 'item201912301913-1.xlsx', 'item2019 [8 characters are OK-!.csv', 'readme.txt'] #Regular expression sample code
For item in items:
    ifre.match(r'item2019.{8}-.\.csv',item):
        print(item)


2022-09-30 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.