赢妥贴前目录下文件,python三取得目录下的文件

作者:计算机知识

os模块下有八个函数:

新建文件,输入DILX570 *.* /B> LIST.TXT,保存。后缀名改为bat

今日三番五次整治原来写的 python 代码,上边是获得文件音讯的 python 管理代码。

在拍卖分类难点创立标签的时候利用了内定文件夹下的公文名获得,在这边做个总括:
OS模块是python中三个常用来拍卖文件音讯的模块,因而相关文书的处理都会用到OS模块。获取文件名常用到的函数为
os.walk((top, topdown = True, onerror = None, followlinks = False))
切切实进行使办法可 参考赢妥贴前目录下文件,python三取得目录下的文件名。 写的特别详细。
os.listdir

  os.walk()

  获取钦定目录下文件的文书名以及文件的多少,然后列出当中还留存的目录名称:

  • 获得内定目录下的文书夹名(不包涵子目录)

  os.listdir()

class="com" style="line-height: 25px; color: #880000;">#!/usr/bin/env python2
style="line-height: 25px; color: #880000;">#-*-encoding:utf-8-*-

style="line-height: 25px; color: #000088;">import class="pln" style="line-height: 25px; color: #000000;"> os class="pun" style="line-height: 25px; color: #666600;">, class="pln" style="line-height: 25px; color: #000000;">sys
style="line-height: 25px; color: #000088;">def style="line-height: 25px; color: #000000;"> listdir class="pun" style="line-height: 25px; color: #666600;">( class="pln" style="line-height: 25px; color: #000000;">dir class="pun" style="line-height: 25px; color: #666600;">, class="pln" style="line-height: 25px; color: #000000;">file class="pun" style="line-height: 25px; color: #666600;">):
style="line-height: 25px; color: #000000;">file class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">write class="pun" style="line-height: 25px; color: #666600;">( class="pln" style="line-height: 25px; color: #000000;">dir style="line-height: 25px; color: #666600;"> style="line-height: 25px; color: #008800;">'n' class="pun" style="line-height: 25px; color: #666600;">)
fielnum style="line-height: 25px; color: #666600;">= style="line-height: 25px; color: #006666;">0
list style="line-height: 25px; color: #666600;">= style="line-height: 25px; color: #000000;"> os style="line-height: 25px; color: #666600;">. style="line-height: 25px; color: #000000;">listdir class="pun" style="line-height: 25px; color: #666600;">( class="pln" style="line-height: 25px; color: #000000;">dir class="pun" style="line-height: 25px; color: #666600;">) class="com" style="line-height: 25px; color: #880000;">#列出目录下的享有文件和目录
style="line-height: 25px; color: #000088;">for style="line-height: 25px; color: #000000;"> line class="kwd" style="line-height: 25px; color: #000088;">in class="pln" style="line-height: 25px; color: #000000;"> list style="line-height: 25px; color: #666600;">:
filepath style="line-height: 25px; color: #666600;">= style="line-height: 25px; color: #000000;"> os style="line-height: 25px; color: #666600;">. style="line-height: 25px; color: #000000;">path class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">join class="pun" style="line-height: 25px; color: #666600;">( class="pln" style="line-height: 25px; color: #000000;">dir class="pun" style="line-height: 25px; color: #666600;">, class="pln" style="line-height: 25px; color: #000000;">line class="pun" style="line-height: 25px; color: #666600;">)
style="line-height: 25px; color: #000088;">if style="line-height: 25px; color: #000000;"> os style="line-height: 25px; color: #666600;">. style="line-height: 25px; color: #000000;">path class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">isdir class="pun" style="line-height: 25px; color: #666600;">( class="pln" style="line-height: 25px; color: #000000;">filepath class="pun" style="line-height: 25px; color: #666600;">): class="com" style="line-height: 25px; color: #880000;">#若果filepath是目录,则再列出该目录下的有所文件
style="line-height: 25px; color: #000000;">myfile class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">write class="pun" style="line-height: 25px; color: #666600;">( class="str" style="line-height: 25px; color: #008800;">' ' class="pun" style="line-height: 25px; color: #666600;"> class="pln" style="line-height: 25px; color: #000000;"> line style="line-height: 25px; color: #666600;"> style="line-height: 25px; color: #008800;">'//' class="pun" style="line-height: 25px; color: #666600;"> class="str" style="line-height: 25px; color: #008800;">'n' class="pun" style="line-height: 25px; color: #666600;">)
style="line-height: 25px; color: #000088;">for style="line-height: 25px; color: #000000;"> li class="kwd" style="line-height: 25px; color: #000088;">in class="pln" style="line-height: 25px; color: #000000;"> os class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">listdir class="pun" style="line-height: 25px; color: #666600;">( class="pln" style="line-height: 25px; color: #000000;">filepath class="pun" style="line-height: 25px; color: #666600;">):
style="line-height: 25px; color: #000000;">myfile class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">write class="pun" style="line-height: 25px; color: #666600;">( class="str" style="line-height: 25px; color: #008800;">' ' class="pun" style="line-height: 25px; color: #666600;"> class="pln" style="line-height: 25px; color: #000000;">li class="pun" style="line-height: 25px; color: #666600;"> class="str" style="line-height: 25px; color: #008800;">'n' class="pun" style="line-height: 25px; color: #666600;">)
fielnum style="line-height: 25px; color: #666600;">= style="line-height: 25px; color: #000000;"> fielnum class="pun" style="line-height: 25px; color: #666600;"> class="lit" style="line-height: 25px; color: #006666;">1
style="line-height: 25px; color: #000088;">elif class="pln" style="line-height: 25px; color: #000000;"> os class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">path class="pun" style="line-height: 25px; color: #666600;">: class="com" style="line-height: 25px; color: #880000;">#要是filepath是文件,间接列出文件名
style="line-height: 25px; color: #000000;">myfile class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">write class="pun" style="line-height: 25px; color: #666600;">( class="str" style="line-height: 25px; color: #008800;">' ' class="pun" style="line-height: 25px; color: #666600;"> class="pln" style="line-height: 25px; color: #000000;">line style="line-height: 25px; color: #666600;"> style="line-height: 25px; color: #008800;">'n' class="pun" style="line-height: 25px; color: #666600;">)
fielnum style="line-height: 25px; color: #666600;">= style="line-height: 25px; color: #000000;"> fielnum class="pun" style="line-height: 25px; color: #666600;"> class="lit" style="line-height: 25px; color: #006666;">1
style="line-height: 25px; color: #000000;">myfile class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">write class="pun" style="line-height: 25px; color: #666600;">( class="str" style="line-height: 25px; color: #008800;">'all the file num is ' style="line-height: 25px; color: #666600;"> style="line-height: 25px; color: #000000;"> str class="pun" style="line-height: 25px; color: #666600;">( class="pln" style="line-height: 25px; color: #000000;">fielnum class="pun" style="line-height: 25px; color: #666600;">))
dir style="line-height: 25px; color: #666600;">= style="line-height: 25px; color: #000000;"> raw_input class="pun" style="line-height: 25px; color: #666600;">( class="str" style="line-height: 25px; color: #008800;">'please input the path:' style="line-height: 25px; color: #666600;">)
myfile style="line-height: 25px; color: #666600;">= style="line-height: 25px; color: #000000;"> open class="pun" style="line-height: 25px; color: #666600;">( class="str" style="line-height: 25px; color: #008800;">'list.txt' class="pun" style="line-height: 25px; color: #666600;">, class="str" style="line-height: 25px; color: #008800;">'w' class="pun" style="line-height: 25px; color: #666600;">)
style="line-height: 25px; color: #000000;">listdir class="pun" style="line-height: 25px; color: #666600;">( class="pln" style="line-height: 25px; color: #000000;">dir class="pun" style="line-height: 25px; color: #666600;">, class="pln" style="line-height: 25px; color: #000000;">myfile class="pun" style="line-height: 25px; color: #666600;">)
style="line-height: 25px; color: #000000;">myfile class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">close class="pun" style="line-height: 25px; color: #666600;">()

 

  获取的文书名、目录音讯会在时下路径下生成 list.txt 的文本。文件内容如下:

path = '/opt'
dirs = os.listdir(path)
for dir in dirs:
    print dir
1 # -*- coding: utf-8 -*-   
2       
3     import os  
4       
5     def file_name(file_dir):   
6         for root, dirs, files in os.walk(file_dir):  
7             print(root) #当前目录路径  
8             print(dirs) #当前路径下所有子目录  
9             print(files) #当前路径下所有非目录子文件

style="line-height: 25px; color: #000000;">G style="line-height: 25px; color: #666600;">: style="line-height: 25px; color: #000000;">codespythonfile_info
style="line-height: 25px; color: #000000;">list class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">txt
style="line-height: 25px; color: #000000;">list2 class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">txt
style="line-height: 25px; color: #000000;">list_filenames class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">py
style="line-height: 25px; color: #000000;">list_filenames2 class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">py
style="line-height: 25px; color: #000000;">list_files class="pun" style="line-height: 25px; color: #666600;"> . class="pln" style="line-height: 25px; color: #000000;">py
style="line-height: 25px; color: #000000;">test class="com" style="line-height: 25px; color: #880000;">//
style="line-height: 25px; color: #000000;">g style="line-height: 25px; color: #666600;">. style="line-height: 25px; color: #000000;">txt
style="line-height: 25px; color: #000000;">mmm style="line-height: 25px; color: #666600;">. style="line-height: 25px; color: #000000;">pptx
style="line-height: 25px; color: #000000;">test2 class="com" style="line-height: 25px; color: #880000;">//
style="line-height: 25px; color: #000000;">list class="pun" style="line-height: 25px; color: #666600;">. class="pln" style="line-height: 25px; color: #000000;">txt
style="line-height: 25px; color: #000000;">test3
all the file num style="line-height: 25px; color: #000088;">is style="line-height: 25px; color: #006666;">9

  • 收获目录中的全部文件夹以及文件名

出口格式为:

 

  当前文件目录路线

  上面是地方程序的晋级版本,遍历了当前目录下的文件夹,继续实践遍历文件名的通令。

 import os  

 def file_name(file_dir):   
   for root, dirs, files in os.walk(file_dir):  
        print(root) #当前目录路径  
        print(dirs) #当前路径下所有子目录  
        print(files) #当前路径下所有非目录子文件 

本文由bwin必赢发布,转载请注明来源

关键词: Python python学习 小技巧