ython读取文件下的所有图像[

python读取文件下的所有图像[python] view plain copy

  1. #-*- coding: UTF-8 -*-   
  2.   
  3. ''''' 
  4. 1、读取指定目录下的所有文件 
  5. 2、读取指定文件,输出文件内容 
  6. 3、创建一个文件并保存到指定目录 
  7. '''  
  8. import os  
  9.   
  10. # 遍历指定目录,显示目录下的所有文件名  
  11. def eachFile(filepath):  
  12.     pathDir =  os.listdir(filepath)  
  13.     for allDir in pathDir:  
  14.         child = os.path.join('%s%s' % (filepath, allDir))  
  15.         print child.decode('gbk'# .decode('gbk')是解决中文显示乱码问题  
  16.   
  17. # 读取文件内容并打印  
  18. def readFile(filename):  
  19.     fopen = open(filename, 'r'# r 代表read  
  20.     for eachLine in fopen:  
  21.         print "读取到得内容如下:",eachLine  
  22.     fopen.close()  
  23.       
  24. # 输入多行文字,写入指定文件并保存到指定文件夹  
  25. def writeFile(filename):  
  26.     fopen = open(filename, 'w')  
  27.     print "\r请任意输入多行文字"," ( 输入 .号回车保存)"  
  28.     while True:  
  29.         aLine = raw_input()  
  30.         if aLine != ".":  
  31.             fopen.write('%s%s' % (aLine, os.linesep))  
  32.         else:  
  33.             print "文件已保存!"  
  34.             break  
  35.     fopen.close()  
  36.   
  37. if __name__ == '__main__':  
  38.     filePath = "D:\\FileDemo\\Java\\myJava.txt"  
  39.     filePathI = "D:\\FileDemo\\Python\\pt.py"  
  40.     filePathC = "C:\\"  
  41.     eachFile(filePathC)  
  42.     readFile(filePath)  
  43.     writeFile(filePathI)  
[python]  view plain  copy
  1.   
[python]  view plain  copy
  1.   

os 模块下有两个函数: os.walk() os.listdir()
[python]  view plain  copy
  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) #当前路径下所有非目录子文件  
[python]  view plain  copy
  1. # -*- coding: utf-8 -*-     
  2.         
  3.     import os    
  4.         
  5.     def file_name(file_dir):     
  6.         L=[]     
  7.         for root, dirs, files in os.walk(file_dir):    
  8.             for file in files:    
  9.                 if os.path.splitext(file)[1] == '.jpeg':    
  10.                     L.append(os.path.join(root, file))    
  11.         return L    
  12.   
  13.   
  14. #其中os.path.splitext()函数将路径拆分为文件名+扩展名  

[python]  view plain  copy
  1. # -*- coding: utf-8 -*-    
  2.     import os    
  3.         
  4.     def listdir(path, list_name):  #传入存储的list  
  5.         for file in os.listdir(path):    
  6.             file_path = os.path.join(path, file)    
  7.             if os.path.isdir(file_path):    
  8.                 listdir(file_path, list_name)    
  9.             else:    
  10.                 list_name.append(file_path)  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值