Python function Exercises

Source: Internet
Author: User

#Encoding=utf-8 fromUrllib.requestImportUrlopenImportRandomImportOS" "1. Define a fuc (URL, folder_path) to get the contents of the URL address, save it to the Folder_path file directory, and randomly generate a file name. " "defSave_url_content (url,folder_path=None):if  not(Url.startswith ('/ http')orUrl.startswith ('https://') ):        returnU'URL address does not conform to specifications'    if  notOs.path.isdir (folder_path):returnU'Folder_path Non-folder'D=urlopen (URL) content=d.read () rand_filename='test_%s'%random.randint (1,1000) File_path=Os.path.join (folder_path,rand_filename) d= Open (File_path,'WB') d.write (content) D.close ()returnFile_pathPrint(Save_url_content ('http://www.baidu.com','tmp'))" "3. Define a func (URL) and analyze how many links are in the URL content. " "defget_url_count (URL):if  not(Url.startswith ('/ http')orUrl.startswith ('https://') ):        returnU'URL address does not conform to specifications'D=urllib.urlopen (URL) content=D.read ()returnLen (Content.split ('<a href='))-1#str to bytes and refinement not solvedPrint(Get_url_count ('http://hi.baidu.com/jxq61/item/149d29cc8d52513d4594168f'))" "2. Define a func (Folder_path), merge all the files under that directory, and generate a all.txt. " "defmerge (Folder_path):if  notos.path.exists (folder_path):return 'Not exists'     forFinchOs.listdir (Folder_path): File_path=Os.path.join (folder_path,f)ifOs.path.isdir (file_path): Merge (File_path)Else: Merge_file= Open ('merge_test','ab+') Content= Open (File_path,'RB'). Read () merge_file.write (content) merge_file.close () merge ('tmp')" "4. Define a func (URL) to get him? After the argument and returns to a dict. " "ImportUrlparse#The Urlparse module mainly splits the URL into 6 parts and returns the tuple. #Urllib.parse.urlparse (urlstring, scheme= ", Allow_fragments=true)defqs (URL): Query=urlparse.urlparse (URL). QueryreturnDict ([(K,v[0]) forKvinchUrlparse.parse_qs (query). Items ()])Print(QS ('http://126.com'))Print(QS ('http://api/api?f=5&g=6&y=5'))Print(QS ('http://api/api?11=53'))


"5. Define a Func (folder) to delete all files under the folder. 50421 ""  

#use recursion to resolvedefDelete (Folder_path):##习题5 if notos.path.exists (folder_path):return 'Not exists' forFinchOs.listdir (Folder_path): File_path=Os.path.join (folder_path,f)ifOs.path.isdir (file_path): Delete (File_path)Else: Os.remove (file_path) Delete ('/TMP/5')

Python function Exercises

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.