Python exercises-List all files in the directory Delete folder

Source: Internet
Author: User

Requirements Description:

1, the current directory has a lot of folders, files, statistics/usr/local/This directory, if it is a folder, to delete
/usr/local/
F1 W1 F2 W2 W3 W4 F4
W1 W2 W10.txt

Requirements Analysis:

1, first list all the files in the directory, with Os.listdir ()

2, determine if it is a file, Os.path.isfile ()

ImportOsf_dir=OS.GETCWD ()#Get current directoryL_dir=os.path.join (F_dir,'usr\local')#stitching into full usr/local pathLis_dir=os.listdir (L_dir)#list all the files in the directory, generate a list#print (Lis_dir) forPinchlis_dir:d1=Os.path.join (l_dir,p)#print (D1)    ifOs.path.isfile (D1) = =FALSE:FD=Os.path.abspath (D1)#print (FD)fd_list=Os.listdir (FD)#print (fd_list)         forWinchFd_list:fw_dir=Os.path.join (fd,w)#print (Fw_dir)Os.remove (fw_dir) os.rmdir (FD)

Python exercises-List all files in the directory Delete folder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.