Python 基礎(chǔ)學(xué)習(xí)

函數(shù)

glob.glob()

實(shí)用的文件匹配庫(kù),可以匹配給給定路徑下的所有pattern,并以列表的形式返回。

查找文件只用到三個(gè)匹配符:

  • *”, 匹配 0 個(gè)或多個(gè)字符;
  • “?”, ”?”匹配單個(gè)字符;
  • “[]”:”[]”匹配指定范圍內(nèi)的字符,如:[0-9]匹配數(shù)字;

*注意:如果文件名以“點(diǎn)”開頭 ,無法被 '*' 和 '?'匹配,如:".card.gif"*

該方法需要一個(gè)參數(shù)用來指定匹配的路徑字符串(字符串可以為絕對(duì)路徑也可以為相對(duì)路徑),其返回的文件名只包括當(dāng)前目錄里的文件名,不包括子文件夾里的文件。

比如:

#是獲得C盤下的所有txt文件:
glob.glob(r’c:*.txt’)

#獲取指定目錄下的所有圖片:
print glob.glob(r"E:/Picture/*/*.jpg")

#使用相對(duì)路徑
glob.glob(r’../*.py’)

#實(shí)例
extensions = ['jpg', 'jpeg', 'JPG', 'JPEG']
file_list = []
dir_name = './data/'
for extension in extensions:
    file_glob = os.path.join(INPUT_DATA, dir_name, "*." + extension)
    file_list.extend(glob.glob(file_glob))
print("Size of this file_list is :",len(file_list))

在win中,不區(qū)分大小寫,所以extensions = ['jpg', 'jpeg']就可以,不然就會(huì)重復(fù)匹配。

sys.exit()

sys.exit()函數(shù)是通過拋出異常的方式來終止進(jìn)程的,也就是說如果它拋出來的異常被捕捉到了的話程序就不會(huì)退出了。


os.模塊系列大集合

OS模塊簡(jiǎn)單的來說它是一個(gè)Python的系統(tǒng)編程的操作模塊,可以處理文件和目錄這些我們?nèi)粘J謩?dòng)需要做的操作。

在自動(dòng)化測(cè)試中,經(jīng)常需要查找操作文件,比如查找配置文件(從而讀取配置文件的信息),查找測(cè)試報(bào)告等等,經(jīng)常會(huì)對(duì)大量文件和路徑進(jìn)行操作,這就需要依賴os模塊。

首先要導(dǎo)入os模塊

import os
help(os) #查看幫助文檔

1、os.getcwd()

查看當(dāng)前所在目錄(路徑);

>>> import os
>>> print(os.getcwd())
/home

2、os.chdir()

切換目錄(路徑);

>>> import os
>>> os.chdir(r'/etc/sysconfig/')
>>> print(os.getcwd())
/etc/sysconfig

>>> import os
>>> os.getcwd()
'/sibcb1/bioinformatics/gaosiqi'
#絕對(duì)路徑可以
>>> os.chdir('/sibcb1/bioinformatics/gaosiqi/haha')
>>> os.getcwd()
'/sibcb1/bioinformatics/gaosiqi/haha'
#相對(duì)路徑也可以
>>> os.chdir('SRX')
>>> os.getcwd()
'/sibcb1/bioinformatics/gaosiqi/haha/SRX'

3、os.curdir、os.pardir

返回當(dāng)前目錄字符串名、返回當(dāng)前目錄的父目錄的字符串名;

>>> import os
>>> print(os.curdir)
.
>>> print(os.pardir)
..

4、os.makedirs()

生成一個(gè)多層遞歸目錄,類似于R 中的tree?

>>> import os
>>> os.makedirs('/home/test/xuan')

5、os.removedirs()

若目錄為空,則刪除,并遞歸到上一級(jí)目錄,如若也為空,則刪除,依次類推;

>>> import os
>>> os.removedirs('test/xuan')

6、os.mkdir()

創(chuàng)建一個(gè)目錄, 一次只能創(chuàng)建一個(gè)目錄

>>> import os
>>> os.mkdir('test')        #只能創(chuàng)建一個(gè)目錄
>>> os.mkdir('abc/123/xxx')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'abc/123/xxx'

7、os.rmdir()

刪除一個(gè)目錄,若目錄不為空則無法刪除,報(bào)錯(cuò);

>>> import os
>>> os.mkdir('test/abc')    #在test下創(chuàng)建一個(gè)abc;
>>> os.rmdir('test')        #無法刪除test;
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 39] Directory not empty: 'test'
>>> os.rmdir('test/abc')    #可以刪除abc,因?yàn)閍bc目錄下為空;

8、os.listdir()

顯示指定目錄下,所有的文件和子目錄,包括隱藏文件;

>>> import os
>>> dirs = os.listdir('/root')
>>> print(dirs)
['.bash_logout', '.bash_profile', '.cshrc', '.tcshrc', '.bashrc', 'full_stack', 'jf_blog', 'node-v8.9.4-linux-x64', '.bash_history', '.config', '.npm', '.pki', '.ssh', '.gitconfig', '.oracle_jre_usage', '.cache', '.python_history', 'douban', 'mysql57-community-release-el7-10.noarch.rpm', '.mysql_history', '.viminfo']

9、os.remove()

刪除文件,不能刪除文件夾;

>>> import os
>>> print(os.listdir('/test'))
['123', 'hello.txt', '.file', 'qwe']
>>> os.remove('/test/.file')
>>> print(os.listdir('/test'))
['123', 'hello.txt', 'qwe']
>>> os.remove('/test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IsADirectoryError: [Errno 21] Is a directory: '/test'

10、os.system()

運(yùn)行shell命令,直接顯示(相當(dāng)于啟動(dòng)一個(gè)全新的shell,然后去執(zhí)行那條命令,命令執(zhí)行完成過后,shell直接退出);

>>> import os
>>> print(os.system('ls /test/'))    #調(diào)用系統(tǒng)命令
123  hello.txt  qwe
0
 
 
[root@server-7 test]# ls
123  hello.txt  qwe

11、os.path.split(path)

將path分割成目錄和文件名二元組返回;;

>>> import os
>>> print(os.path.split('/test/123/abc'))
('/test/123', 'abc')

12、os.path.abspath(path)

返回path規(guī)范化的絕對(duì)路徑;

>>> import os
>>> print(os.path.abspath('abc'))
/test/123/abc

13、os.path.dirname(path)

返回path的目錄;

>>> import os
>>> print(os.path.dirname('/test/123/abc'))
/test/123

14、os.path.basename(path)

返回path最后的文件名(一個(gè)絕對(duì)路徑只返回最后的文件名);

>>> import os
>>> print(os.path.basename('abc'))
abc
>>> print(os.path.basename('/test/123/abc'))
abc

15、os.path.exists(path)

判斷路徑是否存在,如果path存在,返回True;如果不存在,返回Flase;

>>> import os
>>> print(os.path.exists('/test/123/abc'))
True

16、os.path.isfile(path)

判斷是否是一個(gè)文件,如果是則返回True,否則返回False;

>>> import os
>>> print(os.path.isfile('abc'))
True
>>> print(os.path.isfile('/test/qwe'))
False

17、os.path.join(path1,[path2],[path3])

將路徑和文件名分為一個(gè)列表中的兩個(gè)元素,將它們拼起來,若其中有絕對(duì)路徑,則之前的path將會(huì)被刪除.;

>>> import os
>>> print(os.path.join('/test/','hello.txt'))
/test/hello.txt
>>> print(os.path.join('/test/123','hello.txt'))
/test/123/hello.txt

18、os.popen('ls')

相當(dāng)于打開了一個(gè)臨時(shí)的文件存儲(chǔ)打開的目錄(可以賦給變量,字符串的形式);

>>> import os
>>> a = os.popen('ls').read()
>>> a
'douban\nfull_stack\njf_blog\nmysql57-community-release-el7-10.noarch.rpm\nnode-v8.9.4-linux-x64\n'
>>> print(a)
douban
full_stack
jf_blog
mysql57-community-release-el7-10.noarch.rpm
node-v8.9.4-linux-x64
 
 
 
[root@server-7 ~]# ls -l
total 32
drwxr-xr-x 3 root root    36 Jun 23 19:51 douban
drwxr-xr-x 2 root root    22 Jul 10 14:02 full_stack
drwxr-xr-x 7 root root  4096 Jul  5 18:10 jf_blog
-rw-r--r-- 1 root root 25548 Apr  7  2017 mysql57-community-release-el7-10.noarch.rpm
drwxrwxr-x 7  500  500   111 Jun  5 15:04 node-v8.9.4-linux-x64

類型轉(zhuǎn)換

類型轉(zhuǎn)換函數(shù) 轉(zhuǎn)換路徑
float(string) 字符串 -> 浮點(diǎn)值
int(string) 字符串 -> 整數(shù)值
str(integer) 整數(shù)值 -> 字符串
str(float) 浮點(diǎn)值 -> 字符串

退出交互環(huán)境

quit()

#導(dǎo)入sys退出
import sys
sys,exit()
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容