新聞中心
python里面shutil是什么?
shutil 是高級(jí)的文件,文件夾,壓縮包處理模塊。
公司主營(yíng)業(yè)務(wù):網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。創(chuàng)新互聯(lián)是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。創(chuàng)新互聯(lián)推出定興免費(fèi)做網(wǎng)站回饋大家。
1.shutil.copyfileobj(fsrc, fdst[, length])
將文件內(nèi)容拷貝到另一個(gè)文件中
import shutil
shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
2.shutil.copyfile(src, dst)
拷貝文件
shutil.copyfile('f1.log', 'f2.log')
3.shutil.copymode(src, dst)
僅拷貝權(quán)限。內(nèi)容、組、用戶均不變
shutil.copymode('f1.log', 'f2.log')
4.shutil.copystat(src, dst)
僅拷貝狀態(tài)的信息,包括:mode bits, atime, mtime, flags
shutil.copystat('f1.log', 'f2.log')
5.shutil.copy(src, dst)
拷貝文件和權(quán)限
shutil.copy('f1.log', 'f2.log')
6.shutil.copy2(src, dst)
拷貝文件和狀態(tài)信息
shutil.copy2('f1.log', 'f2.log')
7.shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
遞歸的去拷貝文件夾
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
8.shutil.rmtree(path[, ignore_errors[, onerror]])
遞歸的去刪除文件
shutil.rmtree('folder1')
9.shutil.move(src, dst)
遞歸的去移動(dòng)文件,它類似mv命令,其實(shí)就是重命名。
shutil.move('folder1', 'folder3')
10.shutil.make_archive(base_name, format,...)
創(chuàng)建壓縮包并返回文件路徑,例如:zip、tar
創(chuàng)建壓縮包并返回文件路徑,例如:zip、tar
base_name: 壓縮包的文件名,也可以是壓縮包的路徑。只是文件名時(shí),則保存至當(dāng)前目錄,否則保存至指定路徑,
如:www ? ? ? ? ? ? ? ? ? ? ? ?=保存至當(dāng)前路徑
如:/Users/wupeiqi/www =保存至/Users/wupeiqi/
format: 壓縮包種類,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要壓縮的文件夾路徑(默認(rèn)當(dāng)前目錄)
owner: 用戶,默認(rèn)當(dāng)前用戶
group: 組,默認(rèn)當(dāng)前組
logger: 用于記錄日志,通常是logging.Logger對(duì)象
python shell是什么東西
python shell是Python的命令行。
shell中最常用的是ls命令,python對(duì)應(yīng)的寫(xiě)法是:os.listdir(dirname),這個(gè)函數(shù)返回字符串列表,里面是所有的文件名,不過(guò)不包含”.”和”..”。
如果要遍歷整個(gè)目錄的話就會(huì)比較復(fù)雜一點(diǎn),在解釋器里試一下:
os.listdir(”/”)
[’tmp’, ‘misc’, ‘opt’, ‘root’, ‘.autorelabel’, ’sbin’, ’srv’,‘.autofsck’, ‘mnt’, ‘usr’, ‘var’, ‘etc’, ’selinux’, ‘lib’, ‘net’,‘lost+found’, ’sys’, ‘media’, ‘dev’, ‘proc’, ‘boot’, ‘home’, ‘bin’]
就像這樣,接下去所有命令都可以在python的解釋器里直接運(yùn)行觀看結(jié)果。
擴(kuò)展資料:
python shell對(duì)應(yīng)于shutil.copy(src,dest),這個(gè)函數(shù)有兩個(gè)參數(shù),參數(shù)src是指源文件的名字,參數(shù)dest則是目標(biāo)文件或者目標(biāo)目錄的名字。
如果dest是一個(gè)目錄名,就會(huì)在那個(gè)目錄下創(chuàng)建一個(gè)相同名字的文件。與shutil.copy函數(shù)相類似的是shutil.copy2(src,dest),不過(guò)copy2還會(huì)復(fù)制最后存取時(shí)間和最后更新時(shí)間。
不過(guò),shell的cp命令還可以復(fù)制目錄,python的shutil.copy卻不行,第一個(gè)參數(shù)只能是一個(gè)文件。
其實(shí),python還有個(gè)shutil.copytree(src,dst[,symlinks])。參數(shù)多了一個(gè)symlinks,它是一個(gè)布爾值,如果是True的話就創(chuàng)建符號(hào)鏈接。
移動(dòng)或者重命名文件和目錄,shutil.move(src,dst),與mv命令類似,如果src和dst在同一個(gè)文件系統(tǒng)上,shutil.move只是簡(jiǎn)單改一下名字,如果src和dst在不同的文件系統(tǒng)上,shutil.move會(huì)先把src復(fù)制到dst,然后刪除src文件。
參考資料:Python—百度百科
python基礎(chǔ)教程
你可以看黑馬程序員Python入門(mén)教程:
教程學(xué)習(xí)時(shí)間15天
1-3天內(nèi)容:為L(zhǎng)inux基礎(chǔ)命令
4-13天內(nèi)容:為Python基礎(chǔ)教程14-15 天內(nèi)容:為飛機(jī)大戰(zhàn)項(xiàng)目演練
第一階段(1-3天):
該階段首先通過(guò)介紹不同領(lǐng)域的三種操作系統(tǒng),操作系統(tǒng)的發(fā)展簡(jiǎn)史以及Linux系統(tǒng)的文件目錄結(jié)構(gòu)讓大家對(duì)Linux系統(tǒng)有一個(gè)簡(jiǎn)單的認(rèn)識(shí),同時(shí)知道為什么要學(xué)習(xí)Linux命令。然后我們會(huì)正式學(xué)習(xí)Linux命令
1. 文件和目錄命令:ls,cd,touch,mkdir,rm
2. 拷貝和移動(dòng)命令:tree,cp,mv
3. 文件內(nèi)容命令:cat,more,grep
4. 遠(yuǎn)程管理命令:ifconfig,ping,SSH的工作方式簡(jiǎn)介以及ssh命令
5. 用戶權(quán)限及用戶管理命令:chmod,chgrp,useradd,passwd,userdel
6. 軟件安裝及壓縮命令:apt簡(jiǎn)介及命令,tar,gzip壓縮命令,bzip2壓縮命令
7. vim的基本使用
第二階段(4-10天)
該階段我們正式進(jìn)入Python這門(mén)語(yǔ)言的學(xué)習(xí),首先通過(guò)了解Python語(yǔ)言的起源,Python語(yǔ)言的設(shè)計(jì)目標(biāo),Python語(yǔ)言的設(shè)計(jì)哲學(xué),Python語(yǔ)言的優(yōu)缺點(diǎn)和面向?qū)ο蟮幕靖拍?,以及Python語(yǔ)言的執(zhí)行方式,還有Python集成開(kāi)發(fā)環(huán)境PyCharm的使用為我們接下來(lái)的學(xué)習(xí)做鋪墊。
然后我們會(huì)學(xué)習(xí)int,string,float三種簡(jiǎn)單的變量類型,變量間的計(jì)算,變量的輸入輸出,if判斷語(yǔ)句,while循環(huán)語(yǔ)句,for循環(huán)語(yǔ)句,break和continue的使用,函數(shù)的基本使用,模塊的使用,列表,元組,字典三種高級(jí)變量,字符串的常用操作。
接下來(lái)我們會(huì)通過(guò)一個(gè)名片管理系統(tǒng)的案例,把這一階段的知識(shí)進(jìn)行一個(gè)串聯(lián)。在學(xué)習(xí)名片管理系統(tǒng)時(shí),首先我們會(huì)學(xué)習(xí)怎么去搭建這一系統(tǒng)的框架,然后我們會(huì)分別實(shí)現(xiàn)新增名片,顯示全部名片,查詢名片,刪除名片,修改名片這些功能。
最后我們會(huì)學(xué)習(xí)語(yǔ)法的進(jìn)階內(nèi)容,全局變量,局部變量,可變數(shù)據(jù)類型和不可變數(shù)據(jù)類型以及函數(shù)返回多個(gè)值,函數(shù)的缺省參數(shù),多值參數(shù),遞歸的基本使用。
第三階段(11-13天)
該階段我們會(huì)學(xué)習(xí)面向?qū)ο螅∣OP)這一重要的編程思想,首先學(xué)習(xí)的知識(shí)點(diǎn)有類和對(duì)象的基本概念,dir函數(shù),self的作用,初始化方法__init__,內(nèi)置函數(shù)__str__,__del__,單繼承,方法重寫(xiě),私有屬性和方法,多繼承,多態(tài),類屬性,靜態(tài)方法。
然后我們還會(huì)學(xué)習(xí)單例模式這一設(shè)計(jì)模式,異常的捕獲,異常的拋出,from import局部導(dǎo)入,from import導(dǎo)入同名工具, from import導(dǎo)入所有工具,包的使用,制作模塊,pip的使用以及文件的相關(guān)操作。
第四階段(14-15天)
該階段是項(xiàng)目演練階段,我們會(huì)帶領(lǐng)大家通過(guò)使用之前學(xué)習(xí)過(guò)的知識(shí)開(kāi)發(fā)飛機(jī)大戰(zhàn)這一經(jīng)典游戲,項(xiàng)目中分別有游戲窗口,圖像繪制,游戲循環(huán),事件監(jiān)聽(tīng),精靈和精靈組以及創(chuàng)建敵機(jī),創(chuàng)建英雄和發(fā)射子彈,碰撞檢測(cè)等模塊
python一段小函數(shù)解釋
一般我們常見(jiàn)的網(wǎng)址后綴(suffix)是 cn,或者net,或者com,你說(shuō)的就是域名的后綴列表
后面代碼就是用dot把域名分隔開(kāi),
比如 被拆分成[ ‘www','baidu','com']
經(jīng)過(guò)for循環(huán)后,到com的時(shí)候,進(jìn)入if分支,而前面走的是else分支,所以可以看到sdomain的變化如下:
遇到www, sdomain包含['www']
遇到baidu sdomain被替換為['baidu']
遇到com,走if分支,append,變成 ['baidu','com']
然后join后就變成 baidu.com
不過(guò)不知道這么些的理由,要是我,就用正則表達(dá)式,或者直接保留后面兩個(gè)部分
domain=url.split('.')
if domain[-1] in suffixs:
return string.join(domain[-2:],'.')
else:
return None #not valid domain
如何使用python做統(tǒng)計(jì)分析
Shape Parameters
形態(tài)參數(shù)
While a general continuous random variable can be shifted and scaled
with the loc and scale parameters, some distributions require additional
shape parameters. For instance, the gamma distribution, with density
γ(x,a)=λ(λx)a?1Γ(a)e?λx,
requires the shape parameter a. Observe that setting λ can be obtained by setting the scale keyword to 1/λ.
雖然一個(gè)一般的連續(xù)隨機(jī)變量可以被位移和伸縮通過(guò)loc和scale參數(shù),但一些分布還需要額外的形態(tài)參數(shù)。作為例子,看到這個(gè)伽馬分布,這是它的密度函數(shù)
γ(x,a)=λ(λx)a?1Γ(a)e?λx,
要求一個(gè)形態(tài)參數(shù)a。注意到λ的設(shè)置可以通過(guò)設(shè)置scale關(guān)鍵字為1/λ進(jìn)行。
Let’s check the number and name of the shape parameters of the gamma
distribution. (We know from the above that this should be 1.)
讓我們檢查伽馬分布的形態(tài)參數(shù)的名字的數(shù)量。(我們知道從上面知道其應(yīng)該為1)
from scipy.stats import gamma
gamma.numargs
1
gamma.shapes
'a'
Now we set the value of the shape variable to 1 to obtain the
exponential distribution, so that we compare easily whether we get the
results we expect.
現(xiàn)在我們?cè)O(shè)置形態(tài)變量的值為1以變成指數(shù)分布。所以我們可以容易的比較是否得到了我們所期望的結(jié)果。
gamma(1, scale=2.).stats(moments="mv")
(array(2.0), array(4.0))
Notice that we can also specify shape parameters as keywords:
注意我們也可以以關(guān)鍵字的方式指定形態(tài)參數(shù):
gamma(a=1, scale=2.).stats(moments="mv")
(array(2.0), array(4.0))
Freezing a Distribution
凍結(jié)分布
Passing the loc and scale keywords time and again can become quite
bothersome. The concept of freezing a RV is used to solve such problems.
不斷地傳遞loc與scale關(guān)鍵字最終會(huì)讓人厭煩。而凍結(jié)RV的概念被用來(lái)解決這個(gè)問(wèn)題。
rv = gamma(1, scale=2.)
By using rv we no longer have to include the scale or the shape
parameters anymore. Thus, distributions can be used in one of two ways,
either by passing all distribution parameters to each method call (such
as we did earlier) or by freezing the parameters for the instance of the
distribution. Let us check this:
通過(guò)使用rv我們不用再更多的包含scale與形態(tài)參數(shù)在任何情況下。顯然,分布可以被多種方式使用,我們可以通過(guò)傳遞所有分布參數(shù)給對(duì)方法的每次調(diào)用(像我們之前做的那樣)或者可以對(duì)一個(gè)分布對(duì)象凍結(jié)參數(shù)。讓我們看看是怎么回事:
rv.mean(), rv.std()
(2.0, 2.0)
This is indeed what we should get.
這正是我們應(yīng)該得到的。
Broadcasting
廣播
The basic methods pdf and so on satisfy the usual numpy broadcasting
rules. For example, we can calculate the critical values for the upper
tail of the t distribution for different probabilites and degrees of
freedom.
像pdf這樣的簡(jiǎn)單方法滿足numpy的廣播規(guī)則。作為例子,我們可以計(jì)算t分布的右尾分布的臨界值對(duì)于不同的概率值以及自由度。
stats.t.isf([0.1, 0.05, 0.01], [[10], [11]])
array([[ 1.37218364, 1.81246112, 2.76376946],
[ 1.36343032, 1.79588482, 2.71807918]])
Here, the first row are the critical values for 10 degrees of freedom
and the second row for 11 degrees of freedom (d.o.f.). Thus, the
broadcasting rules give the same result of calling isf twice:
這里,第一行是以10自由度的臨界值,而第二行是以11為自由度的臨界值。所以,廣播規(guī)則與下面調(diào)用了兩次isf產(chǎn)生的結(jié)果相同。
stats.t.isf([0.1, 0.05, 0.01], 10)
array([ 1.37218364, 1.81246112, 2.76376946])
stats.t.isf([0.1, 0.05, 0.01], 11)
array([ 1.36343032, 1.79588482, 2.71807918])
If the array with probabilities, i.e, [0.1, 0.05, 0.01] and the array of
degrees of freedom i.e., [10, 11, 12], have the same array shape, then
element wise matching is used. As an example, we can obtain the 10% tail
for 10 d.o.f., the 5% tail for 11 d.o.f. and the 1% tail for 12 d.o.f.
by calling
但是如果概率數(shù)組,如[0.1,0.05,0.01]與自由度數(shù)組,如[10,11,12]具有相同的數(shù)組形態(tài),則元素對(duì)應(yīng)捕捉被作用,我們可以分別得到10%,5%,1%尾的臨界值對(duì)于10,11,12的自由度。
stats.t.isf([0.1, 0.05, 0.01], [10, 11, 12])
array([ 1.37218364, 1.79588482, 2.68099799])
Specific Points for Discrete Distributions
離散分布的特殊之處
Discrete distribution have mostly the same basic methods as the
continuous distributions. However pdf is replaced the probability mass
function pmf, no estimation methods, such as fit, are available, and
scale is not a valid keyword parameter. The location parameter, keyword
loc can still be used to shift the distribution.
離散分布的簡(jiǎn)單方法大多數(shù)與連續(xù)分布很類似。當(dāng)然像pdf被更換為密度函數(shù)pmf,沒(méi)有估計(jì)方法,像fit是可用的。而scale不是一個(gè)合法的關(guān)鍵字參數(shù)。Location參數(shù),關(guān)鍵字loc則仍然可以使用用于位移。
The computation of the cdf requires some extra attention. In the case of
continuous distribution the cumulative distribution function is in most
standard cases strictly monotonic increasing in the bounds (a,b) and
has therefore a unique inverse. The cdf of a discrete distribution,
however, is a step function, hence the inverse cdf, i.e., the percent
point function, requires a different definition:
ppf(q) = min{x : cdf(x) = q, x integer}
Cdf的計(jì)算要求一些額外的關(guān)注。在連續(xù)分布的情況下,累積分布函數(shù)在大多數(shù)標(biāo)準(zhǔn)情況下是嚴(yán)格遞增的,所以有唯一的逆。而cdf在離散分布,無(wú)論如何,是階躍函數(shù),所以cdf的逆,分位點(diǎn)函數(shù),要求一個(gè)不同的定義:
ppf(q) = min{x : cdf(x) = q, x integer}
For further info, see the docs here.
為了更多信息可以看這里。
We can look at the hypergeometric distribution as an example
from scipy.stats import hypergeom
[M, n, N] = [20, 7, 12]
我們可以看這個(gè)超幾何分布的例子
from scipy.stats import hypergeom
[M, n, N] = [20, 7, 12]
If we use the cdf at some integer points and then evaluate the ppf at
those cdf values, we get the initial integers back, for example
如果我們使用在一些整數(shù)點(diǎn)使用cdf,它們的cdf值再作用ppf會(huì)回到開(kāi)始的值。
x = np.arange(4)*2
x
array([0, 2, 4, 6])
prb = hypergeom.cdf(x, M, n, N)
prb
array([ 0.0001031991744066, 0.0521155830753351, 0.6083591331269301,
0.9897832817337386])
hypergeom.ppf(prb, M, n, N)
array([ 0., 2., 4., 6.])
If we use values that are not at the kinks of the cdf step function, we get the next higher integer back:
如果我們使用的值不是cdf的函數(shù)值,則我們得到一個(gè)更高的值。
hypergeom.ppf(prb + 1e-8, M, n, N)
array([ 1., 3., 5., 7.])
hypergeom.ppf(prb - 1e-8, M, n, N)
array([ 0., 2., 4., 6.])
本文標(biāo)題:pythonmv函數(shù) pythonmvc
網(wǎng)站URL:http://fisionsoft.com.cn/article/hidehj.html