2012年9月16日 星期日

python mkdir / rmdir / remove file

# mkdir
os.mkdir('tmpppp')

# rmdir
os.rmdir('tmpppp')

# rm -r
def rm_r(directory):
    for the_file in os.listdir(directory):
        file_path = os.path.join(directory, the_file)
        try:
            if os.path.isfile(file_path):
                os.unlink(file_path)
        except Exception, e:
            print e
    os.rmdir(directory)




Reference :
15.1. os — Miscellaneous operating system interfaces — Python v2.7.3 documentation
file - Delete Folder Contents in Python - Stack Overflow

沒有留言:

張貼留言