달나라 노트

Python ftplib : mkd (make directory) & rmd (remove directory) 본문

Python/Python ftplib

Python ftplib : mkd (make directory) & rmd (remove directory)

CosmosProject 2021. 2. 2. 21:34
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)

ftp.cwd('/test1/test2/test3/')

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

ftp.quit() # quit s3


- Output
Current directory : /
New directory : /test1/test2/test3/

mkd는 directory를 만들어줍니다.

현재 위치 = /

기존에 /test1/test2/라는 디렉토리가 있었고,

test2 디렉토리 안에 test3라는 디렉토리를 생성합니다.

try ~ except ~ 구문을 사용한 것은 혹시나 디렉토리 생성 시 에러가 발생할 경우 에러메세지를 출력한 후 다음 코드가 진행되도록 하기 위함입니다.

 

 

 

 

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

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

ftp.quit() # quit s3


- Output
Current directory : /
New directory : /test1/test2/test3/
Directory is deleted.

rmd로 삭제하길 원하는 디렉토리의 위치를 입력하면 해당 디렉토리가 삭제됩니다.

위 예시에선 /test1/test2/ 안에 있는 test3/ 디렉토리가 삭제됩니다.

 

 

 

 

 

 

728x90
반응형
Comments