如何在Python3.5中使用os模塊、sys模塊和shutil模塊?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

1、os模塊:提供對(duì)操作系統(tǒng)進(jìn)行調(diào)用的接口
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import os
print(os.getcwd()) #獲取當(dāng)前的操作目錄,即當(dāng)前Python腳本工作的目錄路徑
#os.chdir("F:\\PythonCode\\day5\\test") #改變當(dāng)前腳本工作目錄,相當(dāng)于shell下的cd
os.chdir(r"F:\PythonCode\day5\test") #與上面一句等價(jià)(推薦使用)
print(os.getcwd())
print(os.curdir) #返回當(dāng)前目錄 '.'
print(os.pardir) #獲取當(dāng)前目錄的父目錄字符串名 '..'
os.makedirs(r"F:\a\b\c") #生成多層遞歸目錄
#若目錄為空,則刪除,并遞歸到上一級(jí)目錄,如若也為空,則刪除,依此類推
os.removedirs(r"F:\a\b\c") #清理空文件夾
os.mkdir(r"F:\PythonCode\day5\t") #生成單級(jí)目錄,相當(dāng)于shell中mkdir filename
os.rmdir(r"F:\PythonCode\day5\t") #刪除單級(jí)空目錄,若目錄不為空,無(wú)法刪除或報(bào)錯(cuò);相當(dāng)于shell中rmdir filename
print(os.listdir(r"F:\PythonCode\day5\test")) #列出指定目錄下的所有文件和子目錄,包括隱藏文件,并以列表方式打印
os.remove(r"F:\PythonCode\day5\test\1.py") #刪除一個(gè)文件
os.rename(r"F:\PythonCode\day5\test\1.py",r"F:\PythonCode\day5\test\2.py") #重命名文件/目錄
print(os.stat(r"F:\PythonCode\day5\test")) #獲取文件/目錄信息
print(os.sep) #輸出操作系統(tǒng)特定的路徑分隔符,win下為"\\",Linux下為"/"
print(os.linesep) #輸出當(dāng)前平臺(tái)使用的行終止符,win下為"\r\n",Linux下為"\n"
print(os.pathsep) #輸出用于分割文件路徑的字符串,win下為";",Linux下為":"
print(os.environ) #查看系統(tǒng)的環(huán)境變量
print(os.name) #輸出字符串指示當(dāng)前使用平臺(tái)。win->'nt'; Linux->'posix'
print(os.system("dir")) #運(yùn)行shell命令,直接顯示
print(os.path.abspath(r"F:\PythonCode\day5")) #返回path規(guī)范化的絕對(duì)路徑
print(os.path.split(r"F:\PythonCode\day5\test\1.py")) #將path分割成目錄和文件名二元組返回
print(os.path.dirname(r"F:\PythonCode\day5")) #返回path的目錄。其實(shí)就是os.path.split(path)的第一個(gè)元素
print(os.path.basename(r"F:\PythonCode\day5")) #返回path最后的文件名。如何path以/或\結(jié)尾,那么就會(huì)返回空值。
print(os.path.exists(r"F:\PythonCode\day5")) #如果path存在,返回True;如果path不存在,返回False
print(os.path.isabs(r"F:\PythonCode\day5")) #如果path是絕對(duì)路徑,返回True
print(os.path.isfile(r"F:\PythonCode\day5\p_test.py")) #如果path是一個(gè)存在的文件,返回True,否則返回False
print(os.path.isdir(r"F:\PythonCode\day5")) #如果path是一個(gè)存在的目錄,則返回True。否則返回False
print(os.path.join(r"F:",r"\PythonCode",r"\day5",r"\day")) #將多個(gè)路徑組合后返回,第一個(gè)絕對(duì)路徑之前的參數(shù)將被忽略
print(os.path.getatime(r"F:\PythonCode\day5")) #返回path所指向的文件或者目錄的最后存取時(shí)間
print(os.path.getmtime(r"F:\PythonCode\day5")) #返回path所指向的文件或者目錄的最后修改時(shí)間運(yùn)行結(jié)果:
F:\PythonCode\day5
F:\PythonCode\day5\test
.
..
['2.py', 'module_test.py', 'test.py']
os.stat_result(st_mode=16895, st_ino=8162774324625191, st_dev=104211, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1506609480, st_mtime=1506609480, st_ctime=1506579769)
\
;
environ({'PROCESSOR_LEVEL': '6', 'WINDOWS_TRACING_LOGFILE': 'C:\\BVTBin\\Tests\\installpackage\\csilogfile.log', 'PROCESSOR_ARCHITECTURE': 'x86')
nt
?????? F ?е???? ????
??????к??? 0001-9713
F:\PythonCode\day5\test ???¼
2017/09/28 22:38 <DIR> .
2017/09/28 22:38 <DIR> ..
2017/09/28 22:37 69 2.py
2017/09/28 14:31 121 module_test.py
2017/09/28 14:35 237 test.py
3 ????? 427 ???
2 ???¼ 14,051,733,504 ???????
0
F:\PythonCode\day5
('F:\\PythonCode\\day5\\test', '1.py')
F:\PythonCode
day5
True
True
True
True
F:\day
1506656912.210523
1506656912.2105232、sys模塊
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
import sys
print(sys.argv) #命令行參數(shù)List,第一個(gè)元素是程序本身路徑
print(sys.version) #獲取Python解釋程序的版本信息
print(sys.path) #返回模塊的搜索路徑,初始化時(shí)使用PYTHONPATH環(huán)境變量的值
print(sys.platform) #返回操作系統(tǒng)平臺(tái)名稱
sys.stdout.write('please:') #標(biāo)準(zhǔn)輸出,寫入字符串輸出到屏幕
val = sys.stdin.readline()[:-1] #標(biāo)準(zhǔn)輸入
print(val)
sys.exit(0) #退出程序,正常退出時(shí)exit(0)運(yùn)行結(jié)果:
['F:/PythonCode/day5/sys_module.py'] 3.5.2 |Anaconda 4.2.0 (32-bit)| (default, Jul 5 2016, 11:45:57) [MSC v.1900 32 bit (Intel)] ['F:\\PythonCode\\day5', 'F:\\PythonCode', 'C:\\Users\\Administrator\\Anaconda3\\python35.zip', 'C:\\Users\\Administrator\\Anaconda3\\DLLs', 'C:\\Users\\Administrator\\Anaconda3\\lib', 'C:\\Users\\Administrator\\Anaconda3', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\Sphinx-1.4.6-py3.5.egg', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\Pythonwin', 'C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\setuptools-27.2.0-py3.5.egg'] win32 hello please:hello
3、shutil模塊:高級(jí)的文件、文件夾、壓縮包處理模塊
(1)將文件內(nèi)容拷貝到另一個(gè)文件中,可以部分內(nèi)容——shutil.copyfileobj(fsrc, fdst[, length])
f1 = open("p_test.py",encoding="utf-8")
f2 = open("p1.py","w",encoding="utf-8")
#將文件p_test.py內(nèi)容拷貝到另一個(gè)文件p1.py中,可以部分內(nèi)容
shutil.copyfileobj(f1,f2)運(yùn)行結(jié)果:

(2)拷貝文件——shutil.copyfile(src, dst)
import shutil
shutil.copyfile("p1.py","p2.py") #拷貝文件運(yùn)行結(jié)果:

(3)僅拷貝權(quán)限(內(nèi)容、組、用戶均不變)——shutil.copymode(src, dst)
(4)拷貝狀態(tài)的信息,包括:mode bits, atime, mtime, flags——shutil.copystat(src, dst)
(5)拷貝文件和權(quán)限——shutil.copy(src, dst)
(6)拷貝文件和狀態(tài)信息——shutil.copy2(src, dst)
(7)遞歸的去拷貝文件:
shutil.ignore_patterns(*patterns) shutil.copytree(src, dst, symlinks=False, ignore=None)
import shutil
shutil.copytree("test","test1") #遞歸的去拷貝文件運(yùn)行結(jié)果:

(8)遞歸的去刪除文件——shutil.rmtree(path[, ignore_errors[, onerror]])
(9)遞歸的去移動(dòng)文件——shutil.move(src, dst)
(10)創(chuàng)建壓縮包并返回文件路徑,例如:zip、tar——shutil.make_archive(base_name, format,...)
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ì)象
import shutil
shutil.make_archive("shutil_archive_test","zip","F:\PythonCode\day5")運(yùn)行結(jié)果: 
總結(jié):shutil 對(duì)壓縮包的處理是調(diào)用 ZipFile 和 TarFile 兩個(gè)模塊來(lái)進(jìn)行的
import zipfile
z = zipfile.ZipFile("day5.zip","w")
z.write("p1.py")
print("===========")
z.write("p2.py")
z.close()運(yùn)行結(jié)果:
===========

看完上述內(nèi)容,你們掌握如何在Python3.5中使用os模塊、sys模塊和shutil模塊的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝各位的閱讀!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
網(wǎng)站欄目:如何在Python3.5中使用os模塊、sys模塊和shutil模塊-創(chuàng)新互聯(lián)
瀏覽路徑:http://chinadenli.net/article40/edhho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、面包屑導(dǎo)航、做網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、Google、域名注冊(cè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容