數據挖掘之PYTHON OS模塊


數據挖掘之PYTHON OS模塊


python split(),os.path.split()和os.path.splitext()函數用法

將常見的用法總結一下,以防自己在學習過程中忘記。現在分享給大家。

1 # -*- coding:utf-8 -*-

2 """

3 @author:lei

4 """

5 import os

6

7 #os.path.join() 將分離的部分合成一個整體

8 filename=os.path.join('/home/ubuntu/python_coding','split_func')

9 print filename

10 #輸出為:/home/ubuntu/python_coding/split_func

11

12 #os.path.splitext()將文件名和擴展名分開

13 fname,fename=os.path.splitext('/home/ubuntu/python_coding/split_func/split_function.py')

14 print 'fname is:',fname

15 print 'fename is:',fename

16 #輸出為:

17 # fname is:/home/ubuntu/python_coding/split_func/split_function

18 #fename is:.py

19

20 #os.path.split()返回文件的路徑和文件名

21 dirname,filename=os.path.split('/home/ubuntu/python_coding/split_func/split_function.py')

22 print dirname

23 print filename

24 #輸出為:

25 # /home/ubuntu/python_coding/split_func

26 #split_function.py

27

28 #split()函數

29 #string.split(str="", num=string.count(str))[n]

30 #str - - 分隔符,默認為所有的空字符,包括空格、換行(\\n)、製表符(\\t)等。

31 #num - - 分割次數。

32 #[n] - - 選取的第n個分片

33 string = "hello.world.python"

34 print string.split('.')#輸出為:['hello', 'world', 'python']

35 print(string.split('.',1))#輸出為:['hello', 'world.python']

36 print(string.split('.',1)[0])#輸出為:hello

37 print(string.split('.',1)[1])#輸出為:world.python

38 string2="helloandend"

39 print(string2.split("")[0])#輸出為:c++

python split(), os.path.split()和os.path.splitext()函數1. split() split() 函數通過指定分隔符對字符串進行切片,如果參數 num 有指定值,則僅分隔 num 個子…

python中os.walk是一個簡單易用的文件、目錄遍歷器,可以幫助我們高效的處理文件、

目錄方面的事情。

1.載入

要使用os.walk,首先要載入該函數

可以使用以下兩種方法

import os

from os import walk

2.使用

walk(top, topdown=True, onerror=None, followlinks=False)

參數

top 是你所要便利的目錄的地址

topdown 為真,則優先遍歷top目錄,否則優先遍歷top的子目錄(默認為開啟)

onerror 需要一個 callable 對象,當walk需要異常時,會調用

followlinks 如果為真,則會遍歷目錄下的快捷方式(linux 下是 symbolic link)實際所指

的目錄(默認關閉)

os.walk 的返回值是一個生成器(generator),也就是說我們需要不斷的遍歷它,來獲得所

有的內容。

每次遍歷的對象都是返回的是一個三元組(root,dirs,files)

root 所指的是當前正在遍歷的這個文件夾的本身的地址

dirs 是一個 list ,內容是該文件夾中所有的目錄的名字(不包括子目錄)

files 同樣是 list , 內容是該文件夾中所有的文件(不包括子目錄)

如果topdown 參數為真,walk 會遍歷top文件夾,與top文件夾中每一個子目錄。

python中os.walk的用法

a -> b -> 1.txt, 2.txt

c -> 3.txt

d ->

4.txt

5.txt

for (root, dirs, files) in os.walk('a'):

#第一次運行時,當前遍歷目錄為 a

所以 root == 'a'

dirs == [ 'b', 'c', 'd']

files == [ '4.txt', '5.txt']

。。。

# 接著遍歷 dirs 中的每一個目錄

b: root = 'a\\\\b'

dirs = []

files = [ '1.txt', '2.txt']

# dirs為空,返回

# 遍歷c

c: root = 'a\\\\c'

dirs = []

files = [ '3.txt' ]

PS : 如果想獲取文件的全路徑,只需要

for f in files:

path = os.path.join(root,f)

# 遍歷d

d: root = 'a\\\\b'

dirs = []

files = []

遍歷完畢,退出循環

import os

Root = 'a'

Dest = 'b'

for (root, dirs, files) in os.walk(Root):

new_root = root.replace(Root, Dest, 1)

if not os.path.exists(new_root):

os.mkdir(new_root)

for d in dirs:

d = os.path.join(new_root, d)

if not os.path.exists(d):

os.mkdir(d)

for f in files:

# 把文件名分解為 文件名.擴展名

# 在這裡可以添加一個 filter,過濾掉不想複製的文件類型,或者文件名

(shotname, extension) = os.path.splitext(f)

# 原文件的路徑

old_path = os.path.join(root, f)

new_name = shotname + '_bak' + extension

# 新文件的路徑

new_path = os.path.join(new_root, new_name)

try:

# 複製文件

open(new_path, 'wb').write(open(old_path, 'rb').read())

except IOError as e:

print(e)


分享到:


相關文章: