pythonはシステムファイルに対して操作します。

1951 ワード

例えば、pythonオペレーティングシステムのファイルディレクトリは、ネットワーク上でダウンロードされたコンテンツをハードディスクのファイルに保存します。
#!/bin/python3.4
# -*- coding:utf-8 -*-

import os
from requests.exceptions import RequestException
import requests
from hashlib import md5

url = 'http://p3.pstatp.com/origin/1b780001aec9e74e0d9d'
'''    ,                  '''
url1 = 'http://www.baidu.com'
'''    ,                 '''

def download_data(url):
    try:
        response = requests.get(url)
        '''  url,    '''
        if response.status_code == 200:
            print ("    :" + url)
            save_data_local(response.content)
            '''           ,            ,response.text      '''
    except RequestException:
        print ("        ")
        return None

def save_data_local(content):
    path = os.path.join('test')
    if not os.path.exists(path):
        os.mkdir(path)
        '''        '''
    file_path = "{0}/{1}.{2}".format(path,md5(content).hexdigest(),'jpg')
    '''           
        {0}:    ,        ,os.getcwd()         
        {1}:      ,        md5      
        {2}:      
            '''
    if not os.path.exists(file_path):'''      md5   ,      '''
        with open(file_path,'wb') as file:
            '''    ,wb  ,     '''
            file.write(content)
            '''               '''
            file.close()
            '''    '''
def main():
    download_data(url)
if __name__ == '__main__':
    main()
    print ("    ")
pythonのosモジュール:攂1、File Names、Command Line、Agments、and Eviroment Varable e e abers荗2、Process Parameters 3、File Object Cration 4、File Descriptor Operation System Information〹9、Random Numbers