달나라 노트

Python ftplib : nlst (Directory 속 file list) 본문

Python/Python ftplib

Python ftplib : nlst (Directory 속 file list)

CosmosProject 2021. 2. 2. 21:51
728x90
반응형

 

 

 

import ftplib

s3_host = '11.111.111.111'
s3_user_id = 'user_1'
s3_user_password = 'pw_1'

ftp = ftplib.FTP()
ftp.encoding = 'utf-8'
ftp.connect(s3_host) # connect to s3
ftp.login(s3_user_id, s3_user_password) # login to s3


print('Current directory :', ftp.pwd())

# make directory in s3
try:
    ftp.mkd('/test1/test2/test3/')
except Exception as e:
    print(e)

try:
    ftp.mkd('/test1/test2/test4/')
except Exception as e:
    print(e)

ftp.cwd('/test1/test2/')
print('Current directory :', ftp.pwd())

# print file list in a directory
print(ftp.nlst())

# delete directory
try:
    ftp.rmd('/test1/test2/test3/')
    print('Already exists directory is deleted.')
except Exception as e:
    print(e)

print('Current directory :', ftp.pwd())

# print file list in a directory
print('File list :',  ftp.nlst():

ftp.quit() # quit s3


- Output
Current directory : /
Current directory : /test1/test2/
File list : ['test3', 'test4']
Already exists directory is deleted.
Current directory : /test1/test2/
File list : ['test4']

ftplib의 nlst는 현재 디렉토리의 file을 list의 형태로 반환해줍니다.

/test1/test2/ 디렉토리 내부에 test3/, test4/ 디렉토리를 만들었고 nlst를 사용하여 파일 리스트를 출력해보면 ['test3', 'test4']로 나옵니다.

 

도한 rmd로 test3/디렉토리를 삭제한 후 다시 nlst를 사용해보면, test3/가 사라지고 test4/만 나오게 됩니다.

 

 

 

 

 

 

728x90
반응형
Comments