当前位置: 首页>>代码示例>>Python>>正文


Python filetype.guess方法代码示例

本文整理汇总了Python中filetype.guess方法的典型用法代码示例。如果您正苦于以下问题:Python filetype.guess方法的具体用法?Python filetype.guess怎么用?Python filetype.guess使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在filetype的用法示例。


在下文中一共展示了filetype.guess方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: get_filetype

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def get_filetype(this_path):
    
    '''
    获取文件类型
    '''
    
    result = check_fileaccessible(this_path)
    if not result[0] :
        return result
    
    this_path = result[1]
    
    kind = filetype.guess(this_path)
    if kind is not None:
        return (True, kind.extension, kind.mime)
    
    output = os.popen('file --mime-type ' + this_path + ' | gawk {"print $2"}')
    result = output.read()
    result = result.replace('\n', '')
    # /opt/lykops/example/ansible/playbook/ntpdate.yaml: text/plain
    if re.search('text/plain$', result) :
        suffix = 'txt'
    else :
        suffix = os.path.splitext(this_path)[-1]
    return (True, suffix, result) 
开发者ID:lykops,项目名称:lykops,代码行数:27,代码来源:file.py

示例2: Down_load

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def Down_load(file_url, file_full_name, now_photo_count, all_photo_count):
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"}

    # 开始下载图片
    with closing(get(file_url, headers=headers, stream=True)) as response:
        chunk_size = 1024  # 单次请求最大值
        content_size = int(response.headers['content-length'])  # 文件总大小
        data_count = 0 # 当前已传输的大小
        with open(file_full_name, "wb") as file:
            for data in response.iter_content(chunk_size=chunk_size):
                file.write(data)
                done_block = int((data_count / content_size) * 50)
                data_count = data_count + len(data)
                now_jd = (data_count / content_size) * 100
                print("\r %s:[%s%s] %d%% %d/%d" % (file_full_name, done_block * '█', ' ' * (50 - 1 - done_block), now_jd, now_photo_count, all_photo_count), end=" ")

    # 下载完图片后获取图片扩展名,并为其增加扩展名
    file_type = guess(file_full_name)
    rename(file_full_name, file_full_name + '.' + file_type.extension)



# 爬取不同类型图片 
开发者ID:shengqiangzhang,项目名称:examples-of-web-crawlers,代码行数:25,代码来源:crawler_5K_wallpaper.py

示例3: main

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def main():
    f = open('tests/fixtures/sample.jpg', 'rb')
    data = f.read()

    kind = filetype.guess(data)
    if kind is None:
        print('Cannot guess file type!')
        return

    print('File extension: %s' % kind.extension)
    print('File MIME type: %s' % kind.mime) 
开发者ID:h2non,项目名称:filetype.py,代码行数:13,代码来源:buffer.py

示例4: get_file_mimetype

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def get_file_mimetype(filepath):
    _, ext = os.path.splitext(filepath)
    kind = filetype.guess(filepath)
    if kind:
        mime = kind.mime
        ext = '.' + kind.extension
    else:
        try:
            mime = mimetypes.types_map.get(ext)
        except:
            mime = ext = None
    return (mime, ext) 
开发者ID:Wenvki,项目名称:django-idcops,代码行数:14,代码来源:utils.py

示例5: check_type

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def check_type(self):
        self.media_ext = filetype.guess(self.media_path).extension 
开发者ID:instagrambot,项目名称:instapy-cli,代码行数:4,代码来源:media.py

示例6: load

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def load(file_path):
        suffix = Path(file_path).suffix
        # file_type = filetype.guess(file_path)

        if 'csv' in suffix:
            df = pd.read_csv(file_path)
        elif 'xls' in suffix:
            df = pd.read_excel(file_path)
        elif 'h5' in suffix or 'hdf' in suffix:
            df = pd.read_hdf(file_path, encoding="utf-8")
        else:
            print("Invalid file")
            df = None

        return df 
开发者ID:MateLabs,项目名称:AutoOut,代码行数:17,代码来源:data_loader.py

示例7: get_file_type

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def get_file_type(file_path):
    """
    get the type of file according to the suffix of the file name
    :param file_path: file path
    :return:
        file type
    """
    if os.path.exists(file_path):
        file_type = ft.guess(file_path).extension
    else:
        file_type = None

    return file_type 
开发者ID:Charleswyt,项目名称:tf_audio_steganalysis,代码行数:15,代码来源:file_preprocess.py

示例8: main

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def main():
    buf = bytearray([0xFF, 0xD8, 0xFF, 0x00, 0x08])
    kind = filetype.guess(buf)

    if kind is None:
        print('Cannot guess file type!')
        return

    print('File extension: %s' % kind.extension)
    print('File MIME type: %s' % kind.mime) 
开发者ID:h2non,项目名称:filetype.py,代码行数:12,代码来源:bytes.py

示例9: main

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def main():
    kind = filetype.guess('tests/fixtures/sample.jpg')
    if kind is None:
        print('Cannot guess file type!')
        return

    print('File extension: %s' % kind.extension)
    print('File MIME type: %s' % kind.mime) 
开发者ID:h2non,项目名称:filetype.py,代码行数:10,代码来源:file.py

示例10: test_guess_jpeg

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def test_guess_jpeg(self):
        kind = filetype.guess(FIXTURES + '/sample.jpg')
        self.assertTrue(kind is not None)
        self.assertEqual(kind.mime, 'image/jpeg')
        self.assertEqual(kind.extension, 'jpg') 
开发者ID:h2non,项目名称:filetype.py,代码行数:7,代码来源:test_types.py

示例11: test_guess_jpx

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def test_guess_jpx(self):
        kind = filetype.guess(FIXTURES + '/sample.jpx')
        self.assertTrue(kind is not None)
        self.assertEqual(kind.mime, 'image/jpx')
        self.assertEqual(kind.extension, 'jpx') 
开发者ID:h2non,项目名称:filetype.py,代码行数:7,代码来源:test_types.py

示例12: test_guess_heic

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def test_guess_heic(self):
        kind = filetype.guess(FIXTURES + '/sample.heic')
        self.assertTrue(kind is not None)
        self.assertEqual(kind.mime, 'image/heic')
        self.assertEqual(kind.extension, 'heic') 
开发者ID:h2non,项目名称:filetype.py,代码行数:7,代码来源:test_types.py

示例13: test_guess_mp4

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def test_guess_mp4(self):
        kind = filetype.guess(FIXTURES + '/sample.mp4')
        self.assertTrue(kind is not None)
        self.assertEqual(kind.mime, 'video/mp4')
        self.assertEqual(kind.extension, 'mp4') 
开发者ID:h2non,项目名称:filetype.py,代码行数:7,代码来源:test_types.py

示例14: test_guess_png

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def test_guess_png(self):
        kind = filetype.guess(FIXTURES + '/sample.png')
        self.assertTrue(kind is not None)
        self.assertEqual(kind.mime, 'image/png')
        self.assertEqual(kind.extension, 'png') 
开发者ID:h2non,项目名称:filetype.py,代码行数:7,代码来源:test_types.py

示例15: test_guess_tif

# 需要导入模块: import filetype [as 别名]
# 或者: from filetype import guess [as 别名]
def test_guess_tif(self):
        kind = filetype.guess(FIXTURES + '/sample.tif')
        self.assertTrue(kind is not None)
        self.assertEqual(kind.mime, 'image/tiff')
        self.assertEqual(kind.extension, 'tif') 
开发者ID:h2non,项目名称:filetype.py,代码行数:7,代码来源:test_types.py


注:本文中的filetype.guess方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。