最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用Python查看目錄中的文件示例詳解

 更新時(shí)間:2017年08月28日 08:34:25   作者:RustFisher  
這篇文章主要給大家介紹了關(guān)于利用Python查看目錄中的文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

我們?cè)谌粘i_發(fā)中,經(jīng)常會(huì)遇到一些關(guān)于文件的操作,例如,實(shí)現(xiàn)查看目錄內(nèi)容的功能。類似Linux下的tree命令。統(tǒng)計(jì)目錄下指定后綴文件的行數(shù)。

功能是將目錄下所有的文件路徑存入list中??梢约尤牒缶Y判斷功能,搜索指定的后綴名文件。主要利用遞歸的方法來檢索文件。

仿造 tree 功能示例代碼

Python2.7

列出目錄下所有文件

遞歸法

import os
def tree_dir(path, c_path='', is_root=True):
 """
 Get file list under path. Like 'tree'
 :param path Root dir
 :param c_path Child dir
 :param is_root Current is root dir
 """
 res = []
 if not os.path.exists(path):
 return res
 for f in os.listdir(path):
 if os.path.isfile(os.path.join(path, f)):
  if is_root:
  res.append(f)
  else:
  res.append(os.path.join(c_path, f))
 else:
  res.extend(tree_dir(os.path.join(path, f), f, is_root=False))
 return res

下面是加入后綴判斷的方法。在找到文件后,判斷一下是否符合后綴要求。不符合要求的文件就跳過。

def tree_dir_sur(path, c_path='', is_root=True, suffix=''):
 """ Get file list under path. Like 'tree'
 :param path Root dir
 :param c_path Child dir
 :param is_root Current is root dir
 :param suffix Suffix of file
 """
 res = []
 if not os.path.exists(path) or not os.path.isdir(path):
 return res
 for f in os.listdir(path):
 if os.path.isfile(os.path.join(path, f)) and str(f).endswith(suffix):
  if is_root:
  res.append(f)
  else:
  res.append(os.path.join(c_path, f))
 else:
  res.extend(tree_dir_sur(os.path.join(path, f), f, is_root=False, suffix=suffix))
 return res
if __name__ == "__main__":
 for p in tree_dir_sur(os.path.join('E:\ws', 'rnote', 'Python_note'), suffix='md'):
 print p

統(tǒng)計(jì)目錄下指定后綴文件的行數(shù)

僅適用os中的方法,僅檢索目錄中固定位置的文件

# -*- coding: utf-8 -*-
import os
def count_by_categories(path):
 """ Find all target files and count the lines """
 if not os.path.exists(path):
 return
 c_l_dict = dict() # e.g. {category: lines}
 category_list = [cate for cate in os.listdir(path) if
   os.path.isdir(os.path.join(path, cate)) and not cate.startswith('.')]
 for category_dir in category_list:
 line_count = _sum_total_line(os.path.join(path, category_dir), '.md')
 if line_count > 0:
  c_l_dict[category_dir] = line_count
 return c_l_dict
def _sum_total_line(path, endswith='.md'):
 """ Get the total lines of target files """
 if not os.path.exists(path) or not os.path.isdir(path):
 return 0
 total_lines = 0
 for f in os.listdir(path):
 if f.endswith(endswith):
  with open(os.path.join(path, f)) as cur_f:
  total_lines += len(cur_f.readlines())
 return total_lines
if __name__ == '__main__':
 note_dir = 'E:/ws/rnote'
 ca_l_dict = count_by_categories(note_dir)
 all_lines = 0
 for k in ca_l_dict.keys():
 all_lines += ca_l_dict[k]
 print 'all lines:', str(all_lines)
 print ca_l_dict

以筆記文件夾為例,分別統(tǒng)計(jì)分類目錄下文件的總行數(shù),測(cè)試輸出

all lines: 25433
{'flash_compile_git_note': 334, 'Linux_note': 387, 'Algorithm_note': 3637, 'Comprehensive': 216, 'advice': 137, 'Java_note': 3013, 'Android_note': 11552, 'DesignPattern': 2646, 'Python_note': 787, 'kotlin': 184, 'cpp_note': 279, 'PyQt_note': 439, 'reading': 686, 'backend': 1136}

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • Python如何將控制臺(tái)輸出另存為日志文件

    Python如何將控制臺(tái)輸出另存為日志文件

    這篇文章主要介紹了Python如何將控制臺(tái)輸出另存為日志文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • python字符串判斷密碼強(qiáng)弱

    python字符串判斷密碼強(qiáng)弱

    這篇文章主要為大家詳細(xì)介紹了python字符串判斷密碼強(qiáng)弱,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • pytorch 預(yù)訓(xùn)練模型讀取修改相關(guān)參數(shù)的填坑問題

    pytorch 預(yù)訓(xùn)練模型讀取修改相關(guān)參數(shù)的填坑問題

    這篇文章主要介紹了pytorch 預(yù)訓(xùn)練模型讀取修改相關(guān)參數(shù)的填坑問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • python爬蟲之selenium模塊

    python爬蟲之selenium模塊

    本文詳細(xì)講解了python爬蟲之selenium模塊,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • 如何利用Python+OpenCV實(shí)現(xiàn)簡(jiǎn)易圖像邊緣輪廓檢測(cè)(零基礎(chǔ))

    如何利用Python+OpenCV實(shí)現(xiàn)簡(jiǎn)易圖像邊緣輪廓檢測(cè)(零基礎(chǔ))

    輪廓是形狀分析和物體檢測(cè)和識(shí)別的有用工具,下面這篇文章主要給大家介紹了關(guān)于如何利用Python+OpenCV實(shí)現(xiàn)簡(jiǎn)易圖像邊緣輪廓檢測(cè)(零基礎(chǔ))的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Python中關(guān)于logging模塊的學(xué)習(xí)筆記

    Python中關(guān)于logging模塊的學(xué)習(xí)筆記

    在本篇文章里小編給大家整理的是一篇關(guān)于Python中l(wèi)ogging模塊相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以參考下。
    2020-06-06
  • Python rindex()方法案例詳解

    Python rindex()方法案例詳解

    這篇文章主要介紹了Python rindex()方法案例詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 使用Python的Tornado框架實(shí)現(xiàn)一個(gè)Web端圖書展示頁面

    使用Python的Tornado框架實(shí)現(xiàn)一個(gè)Web端圖書展示頁面

    Tornado是Python的一款高人氣Web開發(fā)框架,這里我們來展示使用Python的Tornado框架實(shí)現(xiàn)一個(gè)Web端圖書展示頁面的實(shí)例,通過該實(shí)例可以清楚地學(xué)習(xí)到Tornado的模板使用及整個(gè)Web程序的執(zhí)行流程.
    2016-07-07
  • Python Pytest裝飾器@pytest.mark.parametrize詳解

    Python Pytest裝飾器@pytest.mark.parametrize詳解

    本文主要介紹了Python Pytest裝飾器@pytest.mark.parametrize詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Python  __getattr__與__setattr__使用方法

    Python __getattr__與__setattr__使用方法

    __getattr__和__setattr__可以用來對(duì)屬性的設(shè)置和取值進(jìn)行處理
    2008-09-09

最新評(píng)論

建湖县| 裕民县| 汝城县| 托克逊县| 昌邑市| 乌兰浩特市| 徐闻县| 海丰县| 德钦县| 延边| 云和县| 金昌市| 文安县| 庄河市| 噶尔县| 乌兰浩特市| 特克斯县| 怀柔区| 万宁市| 潮州市| 自治县| 佛冈县| 徐汇区| 云阳县| 临安市| 高安市| 乌拉特前旗| 博白县| 纳雍县| 大余县| 深圳市| 华池县| 德清县| 贵州省| 武陟县| 体育| 龙游县| 龙海市| 广平县| 平山县| 渝北区|