How to check if folder exist inside a directory if not create it using python
Asked Answered
V

3

5

I use the below python script to check if a file exist on the root of my ftp server.

from ftplib import FTP 
ftp = FTP('ftp.hostname.com')
ftp.login('login', 'password')

folderName = 'foldername'

if folderName in ftp.nlst() :
    print 'YES'
else : print 'NO'

How can I modify the above script to look inside a specific folder instead of the root directory?

For example, I want to see if a folder name called foo exists inside the www directory.

The goal of my question, is to see if the folder foo exists inside the www directory, if so print cool! if not create a folder called foo inside www.

Villanovan answered 14/1, 2014 at 22:48 Comment(0)
V
4

After the hint from Hans! I searched on google for those commandes and I found this link : http://docs.python.org/2/library/ftplib.html

from ftplib import FTP 

ftp = FTP('ftp.hostname.com')
ftp.login('login', 'passwrd')
ftp.cwd('www') # change into 'www' directory

if 'foo' in ftp.nlst() : #check if 'foo' exist inside 'www'
    print 'YES'
    ftp.cwd('foo')  # change into "foo" directory
    ftp.retrlines('LIST') #list directory contents

else : 
    print 'NO'
    ftp.mkd('foo') #Create a new directory called foo on the server.
    ftp.cwd('foo') # change into 'foo' directory
    ftp.retrlines('LIST') #list subdirectory contents

ftp.close() #close connection
Villanovan answered 15/1, 2014 at 22:46 Comment(0)
C
6
from ftplib import FTP 
ftp = FTP('ftp.hostname.com')
ftp.login('login', 'password')
where      = 'www'
folderName = 'foldername'

if folderName in ftp.nlst(where) :
    print 'YES'
else :
    print 'NO'

Just send the directory you want to see in as first argument of ftp.nlst()

Copier answered 28/4, 2014 at 9:15 Comment(2)
ftp.nlst() will give you filenames and directories in CWD.Rogan
@TomaszGandor @Copier ftp.nlst(where) does not work properly when there is a space in the directory name. Any workaround?Orji
V
4

After the hint from Hans! I searched on google for those commandes and I found this link : http://docs.python.org/2/library/ftplib.html

from ftplib import FTP 

ftp = FTP('ftp.hostname.com')
ftp.login('login', 'passwrd')
ftp.cwd('www') # change into 'www' directory

if 'foo' in ftp.nlst() : #check if 'foo' exist inside 'www'
    print 'YES'
    ftp.cwd('foo')  # change into "foo" directory
    ftp.retrlines('LIST') #list directory contents

else : 
    print 'NO'
    ftp.mkd('foo') #Create a new directory called foo on the server.
    ftp.cwd('foo') # change into 'foo' directory
    ftp.retrlines('LIST') #list subdirectory contents

ftp.close() #close connection
Villanovan answered 15/1, 2014 at 22:46 Comment(0)
O
1

ftplib is a rather thin wrapper around the FTP protocol. You can look at http://en.wikipedia.org/wiki/List_of_FTP_commands to see what the FTP commands do.

Hint: look at CWD, LIST, MKD.

For LIST you will need ftp.retrlines and parse it to see if it is a directory.

Obadias answered 14/1, 2014 at 22:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.