【Python文件處理】遞歸批處理文件夾子目錄內所有txt數據


因為有個需求,需要處理文件夾內所有txt文件,將txt里面的數據篩選,重新存儲。

雖然手工可以做,但想到了python一直主張的是自動化測試,就想試着寫一個自動化處理數據的程序。

 

一.分析數據格式

需要處理的數據是txt格式存儲的。下圖中一行中的數據依次是,幀、時間、編號、特征點編號、特征點名字、特征點世界坐標x,y,z,特征點屏幕坐標x,y,一共32個特征點,最后6個數據是頭部姿態的位置x,y,z和偏轉角度x,y,z。一行共計233個字段。

需要完成的工作是,把特征點的編號,世界坐標,屏幕坐標分別寫入2個csv文件中。

因為后面需要用到svm分類器,在數據挖掘軟件weka中進行分類。

 

 

二.Python文件讀取操作

  需要做的是Python中txt文件讀取操作,然后利用split()函數將每行的字符串分割成元組,然后利用下標講我們需要保留的數據寫入到新的txt中。

常見的Python文件讀取txt的方法有3種:

  方法一:

1 f = open("foo.txt")             # 返回一個文件對象  
2 line = f.readline()             # 調用文件的 readline()方法  
3 while line:  
4     print line,                 # 后面跟 ',' 將忽略換行符  
5     # print(line, end = '')   # 在 Python 3中使用  
6     line = f.readline()  
7 
8 f.close()  

  方法二:

1 for line in open("foo.txt"):  
2     print line

  方法三:

1 f = open("c:\\1.txt","r")  
2 lines = f.readlines()#讀取全部內容  
3 for line in lines  
4     print line  

 

因為需要處理的數據最后一行不完整,所以,我們只處理到倒數第二行。用readlines()讀取到的是一個List,每行是一個元素。所以可以用len()方法統計有多少行,處理的時候處理到倒數第二行停止。

完成我們的需求:

 1 def readFile(filepath):
 2     f1 = open(filepath, "r")      #打開傳進來的路徑
 3     f_world = open("WorldData", "w")
 4     f_image = open("ImageData", "w")
 5     lines = f1.readlines()        #讀取所有行
 6     lines_count = len(lines)     #統計行數
 7     for n in range(0, lines_count-1):
 8         line = lines[n]
 9         line_object = line.split('\t')
10 
11         i = 5
12 
13         worldposition = ""
14         imageposition = ""
15         while i <= 223:        #取值
16             id = line_object[i - 2]
17             world_x = line_object[i]
18             world_y = line_object[i + 1]
19             world_z = line_object[i + 2]
20             worldposition = id + " " + world_x + world_y + world_z
21             # print worldposition
22             f_world.write(worldposition)
23 
24             image_x = line_object[i + 3]
25             image_y = line_object[i + 4]
26             imageposition = id + " " + image_x + image_y
27             f_image.write(imageposition)
28 
29             i += 7
30         i -= 1
31         headposition = line_object[i - 1] + line_object[i] + line_object[i + 1]
32         headrotate = line_object[i + 2] + line_object[i + 3] + line_object[i + 4]
33         head = headposition + headrotate + "\n"
34 
35         print head
36         f_world.write(head)   # 寫入文件操作
37         f_image.write(head)
38     f1.close()
39     f_image.close()
40     f_world.close()  

 

 

三.遞歸遍歷文件夾下多個txt文件

  因為需要處理的數據不止一個,用自動化的思想來解決這個問題,需要依次遍歷文件夾下多個txt文件。

代碼如下:

 1 def eachFile(filepath):
 2     pathDir = os.listdir(filepath)      #獲取當前路徑下的文件名,返回List
 3     for s in pathDir:
 4         newDir=os.path.join(filepath,s)     #將文件命加入到當前文件路徑后面
 5         if os.path.isfile(newDir) :         #如果是文件
 6             if os.path.splitext(newDir)[1]==".txt":  #判斷是否是txt
 7                 readFile(newDir)                     #讀文件
 8                 pass
 9         else:
10             eachFile(newDir)                #如果不是文件,遞歸這個文件夾的路徑

 

四.對處理得到的文件進行命名

  需求是,處理完一個txt,如abc.txt,然后生成的文件,命名為World_abc.txt和Image_abc.txt,並將生成的文件保存在和源文件同樣的目錄下面。

  思路是,對路徑就行分割,使用

    os.path.split(filepath)

返回的是一個2個元素的元祖,第一個元素是文件夾路徑,第二個是文件名。

1     nowDir = os.path.split(filepath)[0]             #獲取路徑中的父文件夾路徑
2     fileName = os.path.split(filepath)[1]           #獲取路徑中文件名
3     WorldDataDir = os.path.join(nowDir, "WorldData_" + fileName)        #對新生成的文件進行命名的過程
4     ImageDataDir = os.path.join(nowDir, "ImageData_" + fileName)
5 
6     f_world = open(WorldDataDir, "w")
7     f_image = open(ImageDataDir, "w")

處理完之后就嘩嘩嘩生成一大堆了。

 

 

完整代碼:

 1 # -*- coding: utf-8
 2 #讀文件
 3 import os
 4 
 5 #處理文件數量
 6 count=0
 7 
 8 def readFile(filepath):
 9     f1 = open(filepath, "r")
10     nowDir = os.path.split(filepath)[0]             #獲取路徑中的父文件夾路徑
11     fileName = os.path.split(filepath)[1]           #獲取路徑中文件名
12     WorldDataDir = os.path.join(nowDir, "WorldData_" + fileName)        #對新生成的文件進行命名的過程
13     ImageDataDir = os.path.join(nowDir, "ImageData_" + fileName)
14 
15     f_world = open(WorldDataDir, "w")
16     f_image = open(ImageDataDir, "w")
17     lines = f1.readlines()
18     lines_count = len(lines)
19     for n in range(0, lines_count-1):
20         line = lines[n]
21         line_object = line.split('\t')
22 
23         i = 5
24 
25         worldposition = ""
26         imageposition = ""
27         while i <= 223:
28             id = line_object[i - 2]
29             world_x = line_object[i]
30             world_y = line_object[i + 1]
31             world_z = line_object[i + 2]
32             worldposition = id + " " + world_x + world_y + world_z
33             # print worldposition
34             f_world.write(worldposition)
35 
36             image_x = line_object[i + 3]
37             image_y = line_object[i + 4]
38             imageposition = id + " " + image_x + image_y
39             f_image.write(imageposition)
40 
41             i += 7
42         i -= 1
43         headposition = line_object[i - 1] + line_object[i] + line_object[i + 1]
44         headrotate = line_object[i + 2] + line_object[i + 3] + line_object[i + 4]
45         head = headposition + headrotate + "\n"
46 
47         print head
48         f_world.write(head)
49         f_image.write(head)
50     f_world.write("world")
51     f_image.write("image")
52     f1.close()
53     f_image.close()
54     f_world.close()
55 
56     global count
57     count+=1
58 
59 def eachFile(filepath):
60     pathDir = os.listdir(filepath)      #獲取當前路徑下的文件名,返回List
61     for s in pathDir:
62         newDir=os.path.join(filepath,s)     #將文件命加入到當前文件路徑后面
63         if os.path.isfile(newDir) :         #如果是文件
64             if os.path.splitext(newDir)[1]==".txt":  #判斷是否是txt
65                 readFile(newDir)                     #讀文件
66                 pass
67         else:
68             eachFile(newDir)                #如果不是文件,遞歸這個文件夾的路徑
69 
70 
71 eachFile("D:\Python\SVM_Data")
72 print "共處理"+bytes(count)+"個txt"
View Code
 補充:

代碼中添加了一個globle count變量,記錄處理的txt數目,因為python語言是弱類型的語言,在函數中,想要改變全局遍歷的值。

1 count=0
2 def xxx():
3     global count    #聲明這里的count是全局變量count
4     count+=1

最后輸出count數量的時候

1 print "共處理"+count+"個txt"        #報錯,字符類型和值類型不能這樣輸出
2 
3 print "共處理"+bytes(count)+"個txt"        #使用bytes方法,將值類型轉換成字符類型

 

python OS模塊的官方文檔:

10.1. os.path — Common pathname manipulations — Python 2.7.13 documentation  

https://docs.python.org/2.7/library/os.path.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM